Lahey/Fujitsu Fortran

OPEN Statement

Description

The OPEN statement connects or reconnects an external file to an input/output unit.

Syntax

OPEN (connect-specs)
Where:

connect-specs is a comma-separated list of

[UNIT =] external-file-unit IOSTAT=iostat ERR=label FILE=file-name-expr STATUS=status ACCESS=access FORM=form RECL=recl BLANK=blank POSITION=position ACTION=action DELIM=delim PAD=pad BLOCKSIZE=blocksize CONVERT =file-format CARRIAGECONTROL=carriagecontrol

external-file-unit is a scalar INTEGER expression that evaluates to the input/output unit number of an external file.

file-name-expr is a scalar CHARACTER expression that evaluates to the name of a file.

iostat is a scalar default INTEGER variable that is assigned a positive value if an error condition occurs, a negative value if an end-of-file or end-of-record occurs, and zero otherwise.

label is the statement label of the statement that is branched to if an error occurs.

status is a scalar CHARACTER expression. It is NEW if the file does not exist and is to be created; REPLACE if the file is to overwrite an existing file of the same name or create a new one if the file does not exist; SCRATCH if the file is to be deleted at the end of the program or the execution of a CLOSE statement; OLD, if the file is to be opened but not replaced; and UNKNOWN otherwise. The default is UNKNOWN.

access is a scalar CHARACTER expression. It is SEQUENTIAL if the file is to be connected for sequential access, DIRECT if the file is to be connected for direct access, or TRANSPARENT if the file is to be connected for binary access The default value is SEQUENTIAL

form is a scalar CHARACTER expression. It is FORMATTED if the file is to be connected for formatted input/output, UNFORMATTED if the file is to be connected for unformatted input/output, or BINARY if the file is to be connected for binary access. The default value is UNFORMATTED, for a file connected for direct access, and FORMATTED, for a file connected for sequential access.

recl is a scalar default INTEGER expression. It is the record length in bytes for a file connected for direct access, or the maximum record length in bytes for a file connected for sequential access.

blank is a scalar CHARACTER expression. It is NULL if null blank control is used and ZERO if zero blank control is used. The default value is NULL. This specifier is only permitted for a file being connected for formatted input/output.

position is a scalar CHARACTER expression. It is REWIND if the newly opened sequential access file is to be positioned at its initial point; APPEND if it is to be positioned before the endfile record if one exists and at the file terminal point otherwise; and ASIS if the position is to be left unchanged. The default is ASIS. Note that the POSITION keyword may only be used for sequential access files.

action is a scalar CHARACTER expression. It is READ if the file is to be connected for input only, WRITE if the file is to be connected for output only, and READWRITE if the file is to be connected for input and output. The default value is READWRITE.

delim is a scalar CHARACTER expression. It is APOSTROPHE if the apostrophe is used to delimit character constants written with list-directed or namelist formatting, QUOTE if the quotation mark is used, and NONE if neither quotation marks nor apostrophes is used. The default value is NONE. This specifier is permitted only for formatted files and is ignored on input.

pad is a scalar CHARACTER expression. It is YES if the formatted input record is to be padded with blanks and NO otherwise. The default value is YES.

blocksize is a scalar default INTEGER expression. It is the size, in bytes, of the input/output buffer.

file-format is a scalar CHARACTER variable that evaluates to BIG_ENDIAN if big endian conversion is to occur, LITTLE_ENDIAN if little endian conversion is to occur, IBM if IBM style conversion is to occur, and NATIVE if no conversion is to occur.

carriagecontrol is a scalar CHARACTER expression. It is FORTRAN if the first character of a formatted sequential record used for carriage control, and LIST otherwise. Non-storage devices default to FORTRAN; disk files to LIST

Remarks

The OPEN statement connects an existing file to an input/output unit, creates a file that is preconnected, creates a file and connects it to an input/output unit, or changes certain characteristics of a connection between a file and an input/output unit.

If the optional characters UNIT= are omitted from the unit specifier, the unit specifier must be the first item in the connect-spec-list.

If the file to be connected to the input/output unit is the same as the file to which the unit is already connected, only the BLANK=, DELIM=, PAD=, ERR=, and IOSTAT= specifiers can have values different from those currently in effect.

If a file is connected to an input/output unit, it may not be opened with a different unit number.

FILE= is optional if it is the second argument and the first argument is a unit number with no UNIT=.

A unit opened for BINARY or TRANSPARENT access is open for direct access with a record length of one, so REC= may appear, and END= may not appear in any i/o statement for the unit.

Example

integer :: ierr open(8,"inf.dat",status="new") ! creates a new file ! error if file exists open(9,file="open.f90",status="old") ! file must exist open(10,status="scratch") ! file deleted on close open(11,"inf.dat",iostat=ierr) ! ierr returns status ! same file cannot be ! open on two units open(file="foo", & ! if unit is not first, must unit=12, & ! have "unit=" keyword access="direct", & ! direct access requires recl recl=10)