FileOpen function
Open an existing file and return a file handle.
Declaration
function FileOpen(const FileName: UnicodeString; Mode: Integer)
: THandleDescription
FileOpen opens a file with name
FileName with mode
Mode.
Mode can be one of the following constants:
- fmOpenRead
-
Open file in read-only mode.
- fmOpenWrite
-
Open file in write-only mode.
- fmOpenReadWrite
-
Open file in read/write mode.
Under Windows and Unix, the above mode can be or-ed with one of the following sharing/locking flags:
- fmShareCompat
-
Open file in DOS share-compatibility mode.
- fmShareExclusive
-
Lock file for exclusive use.
- fmShareDenyWrite
-
Lock file so other processes can only read.
- fmShareDenyRead
-
Lock file so other processes cannot read.
- fmShareDenyNone
-
Do not lock file.
If the file has been successfully opened, it can be read from or written to
(depending on the Mode parameter) with the FileRead and
FileWrite functions.
There are some limitations to the sharing modes.
- Sharing modes are only available on Unix and Windows platforms.
- Unix only support sharing modes as of 2.4.0.
- fmShareDenyRead only works under Windows at this time, and will always result in an error on Unix platforms because its file locking APIs do not support this concept.
- File locking is advisory on Unix platforms. This means that the locks
are only checked when a file is opened using a file locking mode. In other
cases, existing locks are simply ignored. In particular, this means that
fmShareDenyNonehas no effect under Unix, because this can only be implemented as ``use no locking'' on those platforms. As a result, opening a file using this mode will always succeed under Unix as far as the locking is concerned, even if the file has already been opened usingfmShareExclusive. - Under Solaris, closing a single file handle associated with a file will
result in all locks on that file (even via other handles) being destroyed
due to the behaviour of the underlying API (fcntl). Because of the same reason, on Solaris you cannot use
fmShareDenyWritein combination withfmOpenWrite, norfmShareExclusivein combination withfmOpenReadalthough both work with fmOpenReadWrite.
For an example, see FileCreate
Errors
On Error,
THandle(-1) is returned.
See also
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
