Opening files
A file is opened by sending the open:oflag: message to the CfsFileDescriptor class, which answers a new CfsFileDescriptor instance to act as the receiver for I/O operations to that file. The first argument is a String or DBString specifying the path of the file to be opened; the second is an integer specifying the inclusive-OR of exactly one access mode and zero or more open flags. Access modes and flags are defined in the CfsConstants pool dictionary.
The following table describes open access modes and flags.
Table 10. Open access modes and flags (OR together)
Open access modes
(specify only one)
ORDONLY
Open for reading only
OWRONLY
Open for writing only
ORDWR
Open for reading and writing
Open flags
(specify zero or more)
OAPPEND
Causes the file offset to be set to the end of the file prior to EACH write
OCREAT
Causes the file to be created if it does not exist. Has no effect if the file exists, except as noted under OEXCL.
OEXCL
If OCREAT and OEXCL are both specified, fails if the file exists.
OTRUNC
If the file exists and is a regular file, and the file is successfully opened ORDWR or OWRONLY, causes the file to be truncated to zero length with other platform attributes unchanged.
Some examples of the use of access modes and open flags follow:
| fd |
"Opens an existing file for reading, fails if it does not exist"
(fd := CfsFileDescriptor
open: 'exists.txt'
oflag: ORDONLY) isCfsError
ifTrue: [^self error: fd message].
fd close.
| fd |
"Opens a new file for writing, fails if it exists"
(fd := CfsFileDescriptor
open: 'new.txt'
oflag: OWRONLY | OCREAT | OEXCL) isCfsError
ifTrue: [^self error: fd message].
fd close.
| fd |
"Opens an existing file for reading and writing,
or creates it if it does not exist"
(fd := CfsFileDescriptor
open: 'log.txt'
oflag: ORDWR | OCREAT) isCfsError
ifTrue: [^self error: fd message].
fd close.
| fd |
"Ensures that the opened file is empty: creates if it does not exist,
truncate to size 0 if it does."
(fd := CfsFileDescriptor
open: 'empty.txt'
oflag: ORDWR | OCREAT | OTRUNC) isCfsError
ifTrue: [^self error: fd message].
fd close.
Last modified date: 05/12/2020