April 5, 2000 No. 94
OPEN# File Mode Parameter
The OPEN# language statement opens and, if necessary, creates a data file on a specified stream. The stream must have been previously selected to a native operating system directory with the SELECT# statement. Any file previously open on the stream is first closed. The correct syntax for OPEN# is as follows:
The mode parameter determines the operations that will be performed on the opened file. In addition, an optional translation flag may be appended to the mode to specify how to handle line feeds in an MS-DOS environment (these flags are ignored under UNIX). If a mode parameter is not specified, then a default mode of r+b is assumed, indicating both read and write operations will be performed on a binary file, and that an error will be generated if the file does not exist. The mode parameter and optional translation flags that comprise the file mode argument passed to OPEN# are outlined below.
The following mode parameters passed to OPEN# indicate what operations will be performed on the opened file:
r The file will be opened as read only. The open will fail if the file does not exist.
w The file will be opened as write only. If the file exists then OPEN# overwrites the file and destroys its contents. If the file does not exist, it is created.
a The file will be opened in append mode. OPEN# will create the file if it does not exist. If the file already exists, any data written to the file will be appended to the end of the file. The EOF marker (CTRL-Z) is not moved when a is passed as the file mode parameter, and data will be appended after the EOF marker, if one exists.
r+ The file will be opened for both read and write operations. The open will fail if the file does not exist.
w+ The file will be opened for both read and write operations. If the file exists, OPEN# overwrites the file and destroys its contents. A new file is created if one does not already exist.
a+ The file will be opened for both reading and appending records. A new file is created if one does not already exist. The file pointer can be repositioned, but it is always moved back to the end of the file before any write operation is carried out--existing records cannot be overwritten/updated. The file is checked for an EOF file marker. If an EOF marker exists, it is removed before new data is written to the file and restored after the write process is complete.
Note that in all operations other than r and r+, if the file to be opened does not exist, it is created. If r or r+ is passed as a file mode parameter, if the file does not exist, an error is generated.
A file can be opened in either binary or text mode by appending the following translation flag to the file mode parameter:
b The file will be opened in binary (non-translated) mode. Translations involving carriage-return and linefeed characters are suppressed.
t The file will be opened in text mode. In this mode, CTRL-Z is interpreted as an end-of-file character on input. In addition, in this mode, carriage return-linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return-linefeed combinations on output.
The file mode parameter must begin with the operations mode flag (e.g. a,r,w). If the translation flag is prefixed to the parameter, the function will fail.