Purpose
Open a file.
Syntax
FOpen(, []) —> ptrHandle
Arguments
The file name, including an optional drive, directory, and extension. SetDefault() and SetPath() settings are ignored; the Windows default is used unless you specify a drive and directory as part of the file name. No extension is assumed.
This function sets NetErr() in case of a concurrency control conflict.
The DOS open mode, which determines the accessibility of the file. The open mode is composed of elements from the two types of modes: Access mode + Sharing mode. Specifying an access mode constant indicates how the opened file is to be accessed; the sharing mode determines how other processes can access the file.
Available open and sharing mode constants are listed below:
Access Modes Operation
FO_READ Open for reading (default)
FO_READWRITE Open for reading or writing
FO_WRITE Open for writing
Sharing Modes Operation
FO_COMPAT Compatibility mode (default)
FO_DENYNONE Allow others to read or write
FO_DENYREAD Prevent others from reading
FO_DENYWRITE Prevent others from writing
FO_EXCLUSIVE Exclusive use
FO_SHARED Same as FO_DENYNONE
The default open mode is non-sharable and read-only. If just the access mode is used, the file is opened as non-sharable.
Returns
The file handle of the opened file in the range of 0 to 32,767. This value is similar to an alias in the database system and is required to identify the open file to other file functions. It is, therefore, important to assign the return value to a variable for later use, as in the example below.
If an error occurs, FOpen() returns F_ERROR. FError() can be used to determine the specific error.
Description
FOpen() is a low-level file function that opens an existing file for reading and writing, depending on the argument.
Note that in order for two processes to use the same file simultaneously, both files should be opened in FO_SHARED sharing mode.
Whenever there is an error, FError() can be used to determine the specific error. For example, if the file does not exist, FOpen() returns F_ERROR and FError() returns 2 to indicate that the file was not found.
Examples
This example uses FOpen() to open a file with sharable read/write status and displays an error message if the open fails:
ptrHandle := FOpen(”temp.txt”)
IF ptrHandle = F_ERROR
? DOSErrString(FError())
ENDIF
This example uses FOpen() to open a file with an optional retry if FOpen() fails:
FUNCTION NetOpen(cFile AS STRING,;
wMode AS WORD, wSeconds AS WORD);
AS LOGIC PASCAL
LOCAL lForever AS LOGIC
// Retry forever if wSeconds is zero
lForever := (wSeconds = 0)
DO WHILE (lForever .OR. wSeconds > 0)
IF FOpen(cFile, wMode) != F_ERROR
RETURN TRUE // Open succeeds
ENDIF
InKey(1) // Wait one second
wSeconds -= 1
ENDDO
RETURN FALSE // Open fails
Prototype
FOpen(cFile, wMode) AS PTR CLIPPER
Library
System Library
See Also
FClose(), FCreate(), FError(), FOpen2()