next up previous contents index
Next: CLOSE Statement Up: Input/Output Facilities Previous: Input/Output Statements and Keywords

OPEN Statement


The OPEN statement is used to connect a file to an I/O unit and describe its characteristics. It can open an existing file or create a new one. If the unit is already connected to another file then this is closed before the new connection is made, so that it is impossible to connect two files simultaneously to the same unit. It is an error to try to connect more than one unit simultaneously to the same file. In the special case in which the unit and file are already connected to each other, the OPEN statement may be used to alter the properties of the connection, although in practice only the BLANK= (and sometimes RECL=) values can be changed in this way.

The Fortran Standard does not specify the file position when an existing sequential file is opened. Although most operating systems behave sensibly, in portable software a REWIND statement should be used to ensure that the file is rewound before you read it.

The general form of the OPEN statement is just:
OPEN( control-list )
The control-list can contain any of the following items in any order:

UNIT=integer-expression species the I/O unit number which must be zero or above; the upper limit is system-dependent, typically 99 or 255. The unit identifier must always be given, there is no default value.
STATUS=character-expression describes or specifies the file status. The value must be one of:

'OLD' The file must exist.
'NEW' The file must not already exist, a new file is created.
'SCRATCH' An unnamed temporary file is created; it is deleted automatically when the program exits.
'UNKNOWN' The effect is system-dependent, but usually means that an old file will be used if one exists, otherwise a new file will be created.

The default value is 'UNKNOWN', but it is unwise to omit the STATUS keyword because the effect of 'UNKNOWN' is so ill-defined.

FILE=character-expression specifies the file-name (but any trailing blanks will be ignored). The forms of file-name acceptable are system-dependent: a complete file-specification on some operating systems may include the device, user-name, directory path, file-type, version number etc. and may require various punctuation marks to separate these. In portable software, where the name has to be acceptable to a variety of operating systems, short and simple names should be used. Alternatively the FILE= identifier may be a character variable (or array element) so that the user can choose a file-name at run-time. There is no default for the file-name so one should be specified in all cases unless STATUS='SCRATCH' in which case the file must not be named.

ACCESS=character-expression specifies the file access method. The value may be either:

'SEQUENTIAL' a sequential file: this is the default.
'DIRECT' a direct-access file: in this case the RECL= keyword is also needed.

FORM=character-expression specifies the record format. The value may be either:

'FORMATTED' the default for a sequential file.
'UNFORMATTED' the default for a direct-access file.

RECL=integer-expression specifies the record length. This must be given for a direct-access file but not otherwise. The record-length is measured in characters for a formatted file, but is in system-dependent units (often numeric storage units) for an unformatted file.

BLANK=character-expression specifies how embedded and trailing blanks in numerical input fields of formatted files are to be treated (in the absence of explicit format descriptors BN or BZ). The value may be either:

'NULL' blanks treated as nulls, i.e. ignored: the default.
'ZERO' blanks treated as zeros.

The default value is likely to be the sensible choice in all cases.

IOSTAT=integer-variable (or array element) returns the I/O status code after execution of the OPEN statement. This will be zero if no error has occurred, otherwise it will return a system-dependent positive value.

ERR=label transfers control to the labelled executable statement in the same program unit in the event of an error.

next up previous contents index
Next: CLOSE Statement Up: Input/Output Facilities Previous: Input/Output Statements and Keywords
Helen Rowlands