OpenFile

2.x

  HFILE OpenFile(lpszFileName, lpOpenBuff, fuMode)    
  LPCSTR lpszFileName; /* address of filename */
  OFSTRUCT FAR* lpOpenBuff; /* address of buffer for file information */
  UINT fuMode; /* action and attributes, */  

The OpenFile function creates, opens, reopens, or deletes a file.

Parameters

lpszFileName

Points to a null-terminated string that names the file to be opened. The string must consist of characters from the Windows character set and cannot contain wildcards.

lpOpenBuff

Points to the OFSTRUCT structure that will receive information about the file when the file is first opened. The structure can be used in subsequent calls to the OpenFile function to refer to the open file. The OFSTRUCT structure has the following form:

typedef struct tagOFSTRUCT {    /* of */
   BYTE  cBytes;
   BYTE  fFixedDisk;
   UINT  nErrCode;
   BYTE  reserved[4];
   BYTE  szPathName[128];
} OFSTRUCT;

The szPathName member of OFSTRUCT contains characters from the OEM character set.

For a full description of this structure, see the Microsoft Windows Programmer's Reference, Volume 3.

fuMode

Specifies the action to take and the attributes for the file. This parameter can be a combination of the following values:

Value Meaning

OF_CANCEL Adds a Cancel button to the OF_PROMPT dialog box. Pressing the Cancel button directs OpenFile to return a file-not-found error message.
OF_CREATE Creates a new file. If the file already exists, it is truncated to zero length.
OF_DELETE Deletes the file.
OF_EXIST Opens the file, and then closes it. This value is used to test for file existence. Using this value does not change the file date.
OF_PARSE Fills the OFSTRUCT structure but carries out no other action.
OF_PROMPT Displays a dialog box if the requested file does not exist. The dialog box informs the user that Windows cannot find the file and prompts the user to insert the file in drive A.
OF_READ Opens the file for reading only.
OF_READWRITE Opens the file for reading and writing.
OF_REOPEN Opens the file using information in the reopen buffer.
OF_SEARCH Windows searches in directories even when the file name includes a full path.
OF_SHARE_COMPAT Opens the file with compatibility mode, allowing any program on a given machine to open the file any number of times. OpenFile fails if the file has been opened with any of the other sharing modes.
OF_SHARE_DENY_NONE Opens the file without denying other programs read or write access to the file. OpenFile fails if the file has been opened in compatibility mode by any other program.
OF_SHARE_DENY_READ Opens the file and denies other programs read access to the file. OpenFile fails if the file has been opened in compatibility mode or for read access by any other program.
OF_SHARE_DENY_WRITE Opens the file and denies other programs write access to the file. OpenFile fails if the file has been opened in compatibility or for write access by any other program.
OF_SHARE_EXCLUSIVE Opens the file with exclusive mode, denying other programs both read and write access to the file. OpenFile fails if the file has been opened in any other mode for read or write access, even by the current program.
OF_VERIFY Compares the time and date in the OF_STRUCT with the time and date of the specified file. The function returns HFILE_ERROR if the dates and times do not agree.
OF_WRITE Opens the file for writing only.

Return Value

The return value is an MS-DOS file handle if the function is successful. (This handle is not necessarily valid; for example, if the fuMode parameter is OF_EXIST, the handle does not identify an open file, and if the fuMode parameter is OF_DELETE, the handle is invalid.) The return value is HFILE_ERROR if an error occurs.

Comments

If the lpszFileName parameter specifies a filename and extension only (or if the OF_SEARCH flag is specified), the OpenFile function searches for a matching file in the following directories (in this order):

1.The current directory.

2.The Windows directory (the directory containing WIN.COM), whose path the GetWindowsDirectory function retrieves.

3.The Windows system directory (the directory containing such system files as GDI.EXE), whose path the GetSystemDirectory function retrieves.

4.The directory containing the executable file for the current task; the GetModuleFileName function obtains the path of this directory.

5.The directories listed in the PATH environment variable.

6.The list of directories mapped in a network.

To close the file after use, the application should call the _lclose function.

See Also

GetSystemDirectory, GetWindowsDirectory