To create or open a file and get
access to a
handle for the file, you simply instantiate a TFileStream. This opens or creates a named file and provides methods to read from or write to it. If the file cannot be opened, TFileStream raises an
exception.
constructor Create(const filename: string; Mode: Word);
The Mode parameter specifies how the file should be opened when creating the file stream. The Mode parameter consists of an open mode and a share mode or’ed together. The open mode must be one of the following values:
Table 1.11 Open modes
Value Meaning
fmCreate TFileStream a file with the given name. If a file with the given name exists, open the file in write mode.
fmOpenRead Open the file for reading only.
fmOpenWrite Open the file for writing only. Writing to the file completely replaces the current contents.
fmOpenReadWrite Open the file to modify the current contents rather than replace them.
The share mode can be one of the following values with the restrictions listed below:
Table 1.12 Share modes
Value Meaning
fmShareCompat Sharing is compatible with the way FCBs are opened.
fmShareExclusive Other applications can not open the file for any reason.
fmShareDenyWrite Other applications can open the file for reading but not for writing.
fmShareDenyRead Other applications can open the file for writing but not for reading.
fmShareDenyNone No attempt is made to prevent other applications from reading from or writing to the file.
Note that which share mode you can use depends on which open mode you used. The following table shows shared modes that are available for each open mode.
Open Mode fmShareCompat fmShareExclusive fmShareDenyWrite fmShareDenyRead fmShareDenyNone
fmOpenRead Can’t use Can’t use Available Can’t use Available
fmOpenWrite Available Available Can’t use Available Available
fmOpenReadWrite Available Available Available Available Available
The file open and share mode constants are defined in the SysUtils
unit.