Admin Production ni-theme
Current Publication

OpenFile

LabWindows/CVI

OpenFile

int OpenFile (char fileName[], int readWriteMode, 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 a function with the same name. Include windows.h before including formatio.h to ensure that there are no compilation errors as a result of this naming conflict. Define the SDK_CONFLICT_PRIORITYSDK_CONFLICT_PRIORITY macro to force LabWindows/CVI to use the Windows SDK implementation of this function.

Parameters

Input
Name Type Description
fileName char [] Pathname that specifies the file to open.
readWriteMode int 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 int 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 int 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
fileHandle int 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.

Additional Information

Library: Formatting and I/O Library

Include file: formatio.h

LabWindows/CVI compatibility: LabWindows/CVI 3.0 and later

Example

Refer to apps\daqmthread\daqMT.cws for an example of using the OpenFile function.