OpenFile

int OpenFile (char *fileName, int read/writeMode, int action, int fileType);

Purpose

Opens a file for reading or writing and returns a file handle for use in subsequent file operations.

Note  Remember to call CloseFile when you are finished with operations on the file.
Caution  The Windows SDK also contains an OpenFile function. If you include windows.h and do not include formatio.h, you will get compile errors if you call OpenFile.

Parameters

Input
Name Type Description
fileName string Pathname that specifies the file to open.
read/writeMode integer Specifies what combination of reading and writing you may perform on the file.

If the read/writeMode argument is write or read/write, OpenFile creates the file if it does not already exist. Use GetFileInfo to determine whether a file already exists. OpenFile creates files with full read and write permissions.

  • VAL_READ_WRITE—Open file for reading and writing.
  • VAL_READ_ONLY—Open file for reading only.
  • VAL_WRITE_ONLY—Open file for writing only.
Note  The default value is read only.
action integer Specifies whether to delete the old contents of the file and whether to force the file pointer to the end of the file before each write operation. action is meaningful only if read/writeMode is VAL_READ_WRITE or VAL_WRITE_ONLY. After you perform read operations, the file pointer points to the byte that follows the last byte read. action values are as follows:

  • VAL_TRUNCATE—Positions the file pointer at the beginning of the file and deletes the prior file contents.
  • VAL_APPEND—Appends all write operations to the end of the file.
  • VAL_OPEN_AS_IS—Positions the file pointer at the beginning of the file but does not affect the prior file contents.
fileType integer Specifies whether to treat the data in the file as ASCII or binary. When you perform I/O on a file in binary mode, carriage returns (CR) and linefeeds (LF) receive no special treatment. When you open the file in ASCII mode, each CR/LF combination translates to an LF when reading, and each LF translates to a CR/LF combination when writing. fileType values are as follows:
  • VAL_BINARY
  • VAL_ASCII

Return Value

Name Type Description
handle integer File handle to be used in subsequent ReadFile/WriteFile calls.

If this value is –1, an error occurred. The function failed, was unable to open the file, or a bad argument was passed to the function. You can use GetFmtIOError to get more information about the type of error that occurred.