Reading from a low level file with FoxPro

The FREAD (<filehandle>,<numbytes>) function reads <numbytes> characters from the file. It starts reading from the current position in the file and moves its pointer <numbytes> characters along the file time each time that you call this function. If you ask it to read more bytes than there are in the file then it will return all the bytes available and move the pointer beyond the end of the file. No error will be raised when this happens but you can call the FEOF() function to test whether you have passed the end of the file.

This example reads groups of 10 bytes from a text file and prints them out.

lnFileHandle= FOPEN("file.txt")
DO WHILE NOT FEOF(lnFileHandle)
  ?FREAD(lnFileHandle, 10)
ENDDO
FCLOSE(lnFileHandle)

The FREAD() function treats all characters in the same way. It ignores the special meaning of ASCII characters 10 and 13 (LineFeed and CarriageReturn) and counts them as individual characters. Another low level function, FGETS() will read complete lines of characters and is much more suitable for processing text files.

More tips from Alvechurch Data

More tips from Alvechurch Data

FoxPro Functions

The FREAD and FWRITE functions are more flexible than STRTOFILE and FILETOSTR.

Read More

FoxPro low level files

Using FCLOSE() in FoxPro to close a low level file.

Read More

FoxPro low level files

Using FOPEN() in FoxPro to open a low level file.

Read More