It is possible to open a file dynamically (that is, at run time) by means of
the Fortran OPEN statement.
The OPEN statement will cause a file to become connected. It is used to
describe the properties of a connection in addition to performing the
connection itself. For example, in order to open a text file for input, the
following statement might appear in a program:
It will be apparent that the name of the file and a unit number are used
together with some defaults provided by the system in order to open the file.
The general form of the OPEN statement is:
<olist> is a list of specifiers. It must contain exactly one
external unit specifier <unit> and may contain, at most, one of each of
the other specifiers listed below:
<value> is the unit number. The keyword and equals sign are optional if
this is the first item in the list. UNIT values should be greater than 6 to
avoid the possibility of re-assigning standard input/output streams.
<filename> is the name of the file to which the unit will be connected
when it is opened. This is only optional if the specifier STATUS=SCRATCH is
<variable> is an integer numeric variable into which the input/output
status will be written. The variable will be positive if an error occurred,
otherwise it will be zero.
<label> is a label to jump to in the event of an error.
This specifies the type of access for which the file is to be opened.
<string> is either DIRECT or SEQUENTIAL. As an extension to the Fortran
standard <string> can also be TRANSPARENT (see the FORM specifier below).
By default the ACCESS is SEQUENTIAL.
<string>is a character expression (typically a constant), whose
value is FORMATTED, UNFORMATTED or PRINTER when any trailing blanks are
FORM specifies whether the data transfer to and from the program will be
formatted, unformatted or in line printer format.
If the specifier is omitted, FORMATTED is assumed if the file is being
connected for sequential access and UNFORMATTED is assumed if the file is being
connected for direct or transparent access (see the ACCESS specifier above).
FORM=PRINTER is an extension to the ANSI Standard, and specifies that the first
column of any output record is taken as a Fortran carriage control character.
The Fortran carriage control characters are as follows:
Vertical Spacing before printing
To first line of next page
Carriage return, linefeed, and form feed control
characters are output as necessary to give the effects above.
Note that FORM=PRINTER is only appropriate for files on which output only is
For ACCESS = TRANSPARENT and FORM= FORMATTED, no carriage returns are output at
the end of record on output (the user can output carriage returns with the "/"
editing descriptor), and on input precisely the field widths specified in the
input format are read, with no attempt to align to record boundaries (i.e.
after carriage returns).
For ACCESS = TRANSPARENT and FORM = UNFORMATTED, on output the values in the
I/O list are output to file in their internal format, with no surrounding
record structure (unlike sequential unformatted). Similarly, on input, the
values in the input list are read in direct from file, without any record
structure. This gives a Fortran binding for applications which would previously
have called the I/O primitive subroutines OPENR@, OPENW@, READF@, WRITEF@ etc.
<string> specifies that READ, WRITE or READWRITE operations will be
allowed on the file. The default is READWRITE.
This option may only appear for files being connected for formatted
input/output. <string> is a string which when the trailing spaces are
removed is either NULL or ZERO. The default is NULL. If NULL, then all spaces
in formatted numeric fields are ignored, if ZERO then spaces in formatted
numeric fields are treated as zeros.
<string> specifies the delimiter to be used in character constants. It is
always one of APOSTROPHE , QUOTE or NONE. The default is NONE.
<string> specifies whether input records are space padded. The value is
either YES or NO. The default is YES.
<string> is a character expression that, when trailing spaces have been
removed, is ASIS , REWIND or APPEND.
<value> is an integer expression, which gives record length in bytes. For
direct access files, this specifier is obligatory.
As an extension to the standard, RECL may also be specified for a file opened
for sequential access. This causes fixed length records to be read from or
written to file and allows a BACKSPACE to be followed by a WRITE.
<string> is a character expression which, when trailing spaces are
removed, gives the value OLD, NEW, REPLACE, SCRATCH or UNKNOWN.
The following options for <string> are not in the ANSI Standard:
A FILE= specifier must also be used. 'APPEND' is allowed for both formatted and
unformatted files opened for sequential access. Output is appended to
<filename> if it exists - if <filename> does not exist, it will be
A FILE= specifier must also be used - <filename> need not exist. If
<filename> does not exist, 'MODIFY' is equivalent to 'NEW'. If
<filename> exists, 'MODIFY' causes the existing file to be truncated and
A FILE= specifier must also be used and <filename> must exist. READONLY
status ensures that any attempt to write a record to <filename> causes a
run-time error. It also enables a file to be opened for reading more than once.
The operating system provides a means whereby a program, when opening a file,
can define the access that other programs are allowed to a file for the period
that the first program has the file open.
This file sharing mechanism applies for multiple instances of the same file
opened by a particular program, for two or more programs running on the same
machine, or by two or more programs running on different machines (e.g. access
via a shared disk on a network).
When a program opens a file, it can specify that it requires read access,
write access, or read and write access. In addition to this, it can specify the
<access mode> that other programs are permitted while it still has the
<access mode> is a character expression whose value is one of the
Compatibility mode - equivalent to opening the file with no sharing
attributes. No other program will be able to access the file while this program
has it open.
Exclusive - no other program can access the file while it is
Other programs cannot access the file for write or read/write
access, but can open the file for read only access.
Other programs cannot access the file for read or read/write
access, but can open the file for write only access.
Other programs can access the file for read, write or read/write
Note that a second or subsequent program attempting to open the file will be
denied access in all cases if it attempts to open the file in compatibility
mode. All attempts to open a file that may be in use by another program must
use one of the other modes, and thereby must specify the access to be granted
to other programs trying to access the file subsequently.
DRIVER is an extension to the ANSI Standard. <string> is the name of a
subroutine which has been previously declared in an EXTERNAL statement. If
DRIVER= appears, then FILE=, FILETYPE= and STATUS= must not appear. For further
information see the FTN77 User's Guide available in PDF form on the
FILETYPE is an extension to the ANSI Standard. If FILETYPE='TTY', input
and output is read from the keyboard and written to the screen via the
appropriate unit number.
By default trailing spaces are removed when writing a string to a file. To
avoid this effect use FILETYPE='FSU'.
Note that other devices such as LPT1 may be available by simply opening 'files'
of the corresponding name. For example, to write directly to the printer from
unit 6 it is often possible to execute: OPEN(UNIT=6,FILE='LPT1').
Reading tab characters
In a Fortran READ statement, by default, tab characters read from a file
are converted to spaces. To avoid this conversion you should make a call to the
subroutine READ_TABS@(unitno) immediately after the OPEN statement (unitno
is the unit number of the stream).