Partager via


FOPEN( ) Function

Opens a file for use with low-level file functions.

FOPEN(cFileName [, nAttribute])

Return Values

Numeric

Parameters

  • cFileName
    Specifies the name of the file to open. cFileName can include a path to open files in directories, folders, drives, or volumes not in the current Microsoft Visual FoxPro search path. If a path isn't included, Visual FoxPro searches for the file in the following locations:

    • Default directory

    • Path established with SET PATH

      Note   Visual FoxPro will not recognize a path name properly if a disk or directory name contains an exclamation point (!).

  • nAttribute
    Specifies read-write privileges or a buffering scheme for the file you open. The following table lists each number you can include in nAttribute, and the read-write file privileges and buffering scheme it establishes.

    nAttribute Read-Write privileges Buffered/unbuffered
    0 (Default) Read Only Buffered
    1 Write-Only Buffered
    2 Read and Write Buffered
    10 Read-Only Unbuffered
    11 Write-Only Unbuffered
    12 Read and Write Unbuffered

    If nAttribute isn't included, or if nAttribute evaluates to 0, the file is opened as read-only and is buffered.

    Note   Visual FoxPro will not recognize a path name properly if a disk or directory name contains an exclamation point (!).

Remarks

If FOPEN( ) successfully opens the file, the file handle number of the file is returned. FOPEN( ) returns –1 if the file cannot be opened.

Tip   Assign the file handle number to a memory variable so you can access the file by the memory variable in other low-level file functions.

The following information about files opened with FOPEN( ) can be displayed or sent to a printer with DISPLAY STATUS or LIST STATUS.

  • Drive and directory or volume and folder, and file name
  • File handle number
  • File pointer position
  • Read-write attributes

Example

IF FILE('errors.txt')  && Does file exist? 
   gnErrFile = FOPEN('errors.txt',12)  && If so, open read-write
ELSE
   gnErrFile = FCREATE('errors.txt')  && If not, create it
ENDIF
IF gnErrFile < 0  && Check for error opening file
   WAIT 'Cannot open or create output file' WINDOW NOWAIT
ELSE  && If no error, write to file
   =FWRITE(gnErrFile, 'Error information to be written here')
ENDIF
=FCLOSE(gnErrFile)  && Close file
MODIFY FILE errors.txt NOWAIT  && Open file in edit window 

See Also

CLOSE ALL | FCHSIZE( ) | FCLOSE( ) | FCREATE( ) | FEOF( ) | FFLUSH( ) | FGETS( ) | FPUTS( ) | FREAD( ) | FSEEK( ) | FWRITE( )