FOPEN

Purpose:

Opens a file in a specified read, write or append mode.

Syntax:

FOPEN("filename", "mode")

"filename"

-

A string, the name of the file to open.

"mode"

-

A string, the file mode:

"r"

:

Open the file for read access; if the file does not exist, FOPEN fails.

"w"

:

Open the file for write access; if the file exists, its contents are overwritten; if the file does not exist, it is created.

"a"

:

Open the file for appending; if the file does not exist, it is created.

"r+"

:

Open the file for read and write access; if the file does not exist, FOPEN fails.

"w+"

:

Open the file for read and write access; if the file exists, its contents are overwritten; if the file does not exist, it is created.

"a+"

:

Open the file for read and append access; if the file does not exist, it is created.

"ab"

 

Open the file for binary appending; if the file does not exist, it is created.

"rb"

:

Open the file for binary read access; if the file does not exist, FOPEN fails.

"wb"

:

Open the file for binary write access; if the file exists, its contents are overwritten; if the file does not exist, it is created.

Returns:

A 1 if the open is successful; otherwise 0.

Example:

fopen("header.hdr", "w+")

 

opens the file header.hdr for read and write access, and returns 1 if the open is successful.

Example:

fopen("myfile.dat", "rb")

 

opens the file myfile.dat for binary read access, and returns 1 if the open is successful.

Remarks:

It is recommended to use an FCLOSE or FCLOSEALL with any files opened with FOPEN before exiting.

For binary files, the "ab" or "rb" or "wb" modes are required under Windows to prevent the FREADB and FWRITEB functions from translating a [CR] into a [CR]-[LF] when reading or writing binary data  (i.e., these modes prevent the binary value 10 from being translated into a 10 and a 13).

See Also:

FCLOSE

FCLOSEALL

FGETS

FILEEXISTS

FPRINTF

FPUTS

FSEEK

FSTAT

FTELL