FREAD( ) Function

Returns a specified number of bytes from a file opened with a low-level function.

FREAD(nFileHandle, nBytes)

Parameters

  • nFileHandle
    Specifies the file handle number for the file from which FREAD( ) returns data. You can obtain nFileHandle from the return value of successful FOPEN( ) or FCREATE( ) statements.
  • nBytes
    Specifies the number of bytes returned by FREAD( ) with a limit of 65,535 bytes. FREAD( ) returns data starting from the current file pointer position and continues until it returns nBytes bytes or until it encounters the end of the file.

Return Values

Character data type

Example

The following example uses FREAD( ) to display the contents of a file. If the file is empty, a message is displayed. Before using this example, you must create a sample text file called Test.txt.

Local gnFileHandle,nSize,cString
gnFileHandle = FOPEN("test.txt")
* Seek to end of file to determine number of bytes in the file.
nSize =  FSEEK(gnFileHandle, 0, 2)     && Move pointer to EOF
IF nSize <= 0
 * If file is empty, display an error message.
 WAIT WINDOW "This file is empty!" NOWAIT
ELSE
 * If file is not empty, store the file's contents in memory
 * and display the text in the main Visual FoxPro window.
 = FSEEK(gnFileHandle, 0, 0)      && Move pointer to BOF
 cString = FREAD(gnFileHandle, nSize)
 ? cString
ENDIF
= FCLOSE(gnFileHandle)         && Close the file

See Also

FCHSIZE( ) | FCLOSE( ) | FCREATE( ) | FEOF( ) | FFLUSH( ) | FGETS( ) | FILETOSTR( ) | FOPEN( ) | FPUTS( ) | FSEEK( ) | FWRITE( )