Click or drag to resize

FOpen Function (String, DWord)

X#
Open a file.

Namespace:  XSharp.Core
Assembly:  XSharp.Core (in XSharp.Core.dll) Version: 2.19
Syntax
 FUNCTION FOpen(
	cFileName AS STRING,
	kMode AS DWORD
) AS IntPtr
Request Example View Source

Parameters

cFileName
Type: String
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.
kMode
Type: DWord
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.

Return Value

Type: IntPtr
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.
Remarks
FOpen() is a low-level file function that opens an existing file for reading and writing, depending on the kMode 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.
Remarks
Tip Tip
The low level File IO functions in the X# runtime are using .Net filestreams in the background.
That means that the file handles returned by FOpen() and FCreate() are not 'normal' file handles, but unique identifiers that are used to find the underlying stream object in a collection of streams in the runtime.
That also means that you can't use file handles for functions such as FRead() and FWrite() that were not created in the X# runtime.
If you want to access the underlying FileStream, then you should call the function FGetStream(IntPtr)
Examples
This example uses FOpen() to open a file with sharable read/write status and displays an error message if the open fails:
X#
1ptrHandle := FOpen("temp.txt")
2IF ptrHandle = F_ERROR
3    ? DOSErrString(FError())
4ENDIF
This example uses FOpen() to open a file with an optional retry if FOpen() fails:
X#
 1FUNCTION NetOpen(cFile AS STRING,;
 2        wMode AS DWORD, wSeconds AS DWORD);
 3        AS LOGIC PASCAL
 4    LOCAL lForever AS LOGIC
 5    // Retry forever if wSeconds is zero
 6    lForever := (wSeconds = 0)
 7    DO WHILE (lForever .OR. wSeconds > 0)
 8        IF FOpen(cFile, wMode) != F_ERROR
 9        RETURN TRUE          // Open succeeds
10        ENDIF
11        InKey(1)            // Wait one second
12        wSeconds -= 1
13    ENDDO
14    RETURN FALSE            // Open fails
See Also