21. 4GL Statement Glossary : WriteFile( )
 
Share this page                  
WriteFile( )
Writes a file.
Syntax
[returnfield = ] [callproc] writefile( handle =
  handle
  item {, item } )
returnfield
Specifies the name of field to which the result is returned. Integer.
handle
Specifies the file identifier used by the 4GL file access functions. Integer.
item
Specifies the data that you are writing to the file. If item is a variable, 4GL writes as many bytes as are in the item's data type.
Description
The writefile() function writes data to a file that was previously opened with the openfile() function. The file must be open and the file's handle must be known to 4GL.
For files of type "text," you can only write string data types. Trailing blanks are removed from text and varchar fields. Null values are not accepted. You must convert null values with the ifnull function. For example:
writefile (handle = handle, 
    ifnull(:desc, 'null indicator text'));
For a binary type file, the total size of the items written to the file must be the same as the file's record size. You can declare the record size in the openfile() statement, otherwise 4GL calculates it from the item list size indicated with the first read or write to the file.
The procedure returns 0 if the function completes without error.
Example
In this example, each row of the description column in the table tbl is unloaded into the variable one_row. Each row is then written to the file with the handle file_no.
unloadtable tbl (one_row = description) 
begin
  if (tbl._state = 4) then 
    endloop; 
  endif;
  status = callproc writefile(handle = file_no, one_row);
  if status != 0 then 
    endloop;   
  endif;
end;