Next: READ Statement
Up: Input / Output
Previous: Input / Output
The OPEN statement is used to connect a named file to a logical unit. It is
often possible to pre-connect a file before the program has begun, if
this is the case then there is no need for an OPEN statement, however,
there are many default I/O settings many of which are processor
dependent. Its good practice not to rely on defaults but to specify
exactly what is required in an explicit OPEN statement. This
will make the program more portable.
The syntax is,
OPEN([UNIT=]< integer >, FILE=< filename >, ERR=< label >, &
STATUS=< status >, ACCESS=< method >, ACTION=< mode >, RECL=< int-expr >)
where,
- UNIT=< integer > specifies a numeric reference for the
named file. The number must be one that is not currently in use.
- FILE=< filename > gives the filename to be associated
with the logical unit. The name must match exactly with the actual file,
sometimes this means that the filename needs to contain a specific number of
blanks.
- ERR=< label > specifies a numeric label where control
should be transferred if there is an error opening the named file.
- STATUS=< status > specifies the status of the named
file, the status may be one of the following,
- 'OLD', -- file exists.
- 'NEW', -- file does not exist.
- 'REPLACE' -- file will be overwritten.
- 'SCRATCH' -- file is temporary and will be deleted when
closed.
- 'UNKNOWN' -- unknown.
- ACCESS=< method > specifies the access method,
- 'DIRECT' -- the file consists of tagged records accessed
by an ID number, in this case the record
length must be specified (RECL). Individual records can be specified
and updated without altering the rest of the file.
- 'SEQUENTIAL' -- the file is written / read (sequentially)
line by line.
- ACTION=< mode > specifies what can be done to the file,
the mode may be one of the following,
- 'READ',-- open for reading.
- 'WRITE'-- open for writing.
- 'READWRITE' -- open for both reading and writing.
There now follows an example of use,
OPEN(17,FILE='output.dat',ERR=10, STATUS='REPLACE', &
ACCESS='SEQUENTIAL',ACTION='WRITE')
A file output.dat is opened for writing, it is connected to
logical unit number 17. The file is accessed on a line by line basis and
already exists but is to be replaced. The label 10 must pertain
to a valid executable statement.
OPEN(14,FILE='input.dat',ERR=10, STATUS='OLD', RECL=iexp, &
ACCESS='DIRECT',ACTION='READ')
Here a file is opened for input only on unit 14.
The file is directly accessed and (clearly) already exists.
Return to corresponding overview page
Next: READ Statement
Up: Input / Output
Previous: Input / Output
©University of Liverpool, 1997
Wed May 28 20:20:27 BST 1997Not for commercial use.