FWrite Function

Action

Writes data to a file, starting at the position indicated by the file pointer.

Include file

Kernel.bdh

Syntax

FWrite( in  hFile      : number,
        in  sData      : string,
        in  nDataSize  : number optional,
        in  nWriteMode : number optional,
        out nWritten   : number optional ): boolean;

Return value

  • true if successful

  • false otherwise

Parameter Description
hFile Valid handle to an open file. This handle must have been returned from a previous call to FOpen.
sData Buffer containing data to write
nDataSize Number of bytes to write (optional, default = STRING_COMPLETE).
nWriteMode

Specifies how the data is written to the file (optional). Possible values are:

  • OPT_FILE_APPEND. Appends the data at the end of the file

  • OPT_FILE_INSERT. Inserts the data at the current position

  • OPT_FILE_OVERWRITE. Overwrites the data at the current position (default)

nWritten Variable receiving the number of bytes actually written (optional)

Example

dcltrans
  transaction TMain
var
  hFile, nWritten  : number;
  sOutput          : string;
begin
  // create a file in the temporary directory
  FOpen(hFile, "c:\\temp\\dummyfile.txt", OPT_FILE_ACCESS_READWRITE, OPT_FILE_CREATE);
  write("new file created"); writeln;

  // write string to open file
  sOutput := "Hello world!";
  FWrite(hFile, sOutput, STRING_COMPLETE, OPT_FILE_APPEND, nWritten);
  write (nWritten); write(" bytes written to file"); writeln;

  // close file
  FClose(hFile);
  write("file closed"); writeln;
end TMain;

Output

new file created
12 bytes written to file
file closed