Open a low level file

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.

Access Tips

FoxPro Tips

General Tips

 

Related Items

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

Functions for manipulating text in FoxPro

Foxpro commands and functions for text

Read More

Close a low level file in FoxPro

Use FCLOSE() to close a low level file in FoxPro

Read More

Create a low level file with FoxPro

Use FCREATE() to create a low level file in FoxPro

Read More

Read from a low level file in FoxPro

Use FREAD() to read data from a low level file in FoxPro

Read More