FGETS( ) Function
Collapse the table of content
Expand the table of content

FGETS( ) Function

Returns a series of bytes from a file or a communication port opened with a low-level file function until it encounters a carriage return.

FGETS(nFileHandle [, nBytes])



Specifies the numeric file handle of the file or communication port from which FGETS( ) returns data.


Specifies the number of bytes FGETS( ) returns. FGETS( ) returns nBytes bytes unless a carriage return is encountered first. FGETS( ) returns data between the starting file-pointer position and the carriage return if a carriage return is encountered within nBytes bytes.

FGETS( ) returns a maximum of 8192 bytes. If you omit nBytes, FGETS( )returns 254 bytes by default.


You can read a file line by line by issuing a series of FGETS( ).

FGETS( ) returns a series of bytes as a character string. Data is returned starting from the current file's pointer position and continuing until a carriage return is encountered. The file pointer is then positioned on the byte immediately following the carriage return. The carriage return is not returned as part of the string, and line feeds are discarded.

*** TEST.TXT must exist ***
STORE FOPEN('test.txt') TO gnFileHandle    && Open the file
STORE FSEEK(gnFileHandle, 0, 2) TO gnEnd    && Move pointer to EOF
STORE FSEEK(gnFileHandle, 0) TO gnTop     && Move pointer to BOF
IF gnEnd <= 0  && Is file empty?
   WAIT WINDOW 'This file is empty!' NOWAIT
ELSE  && If not
   gcString = FGETS(gnFileHandle, gnEnd)  && Store contents
   ? gcString
= FCLOSE(gnFileHandle)  && Close the file

Community Additions

© 2016 Microsoft