Open a low level file


The FOPEN() function opens an existing file and returns a file handle. As before, 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.


Other low level topics

Low level overview | FCREATE() | FCLOSE() | FREAD() | FWRITE()


Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map