Open a low level file with FoxPro

The FOPEN() function in the example below opens an existing file and returns a file handle. It will return -1 if it cannot open the file so a real application must always check that the function has succeeded:

*-- Open the file ...
lnFileHandle = FOPEN ('fred.txt')
*-- ... write some more text ...
FWRITE (lnFileHandle, 'Hello world')
*-- ... and close it again.
FCLOSE (lnFileHandle)

By default FOPEN() will create a file with Read-only access. You can still try to write data to the file with FWRITE() but no data will be written and no error will be raised.

You can pass a second parameter to the function and force the file to be Read/Write or Write Only.

Be careful.

The numeric codes for this function are not the same as the codes for the FCREATE() function.

Warning

You will be overwriting the contents of a file that you have opened with FOPEN(). If you want to append to the file then you have to read the original contents of the file into memory, add to that string and write the modified string back to the file.

MS Access technical tips

Visual FoxPro technical tips

General Tips

 

More tips from Alvechurch Data

FoxPro Functions

FoxPro has always had functions like FREAD and FWRITE to read and write files at a low level. They can handle files which defeat the STRTOFILE and FILETOSTR functions.

Read More

FoxPro functions for manipulating text

Foxpro commands and functions for text

Read More

Close a low level file in FoxPro

FoxPro file handling - use FCLOSE() to close a low level file

Read More

Create a low level file with FoxPro

FoxPro file handling - use FCREATE() to create a low level file

Read More

Read from a low level file in FoxPro

FoxPro file handling - use FREAD() to read data from a low level file

Read More