Inputs data from external and internal files.
Syntax
The syntax of the READ
statement can take one of the following forms:
Long form (for use when reading from
a connected file):
READ (io-specifier-list) [input-list] |
Short form (for use when reading from standard input):
READ format [, input-list] |
Short namelist-directed form (for use when reading
from standard input into a namelist group):
- io-specifier-list
is a list of the following comma-separated I/O
specifiers:
- [UNIT=]unit
specifies the unit connected to the input file.
unit can be one of the following:
The name of a character variable,
indicating an internal file
An integer expression that evaluates to the unit
connected to an external file
An asterisk, indicating a pre-connection to unit
5 (standard input)
If the optional keyword UNIT=
is omitted, unit must be the first item
in io-specifier-list.
- [FMT=]format
specifies the format specification for formatting
the data. format can be one of the following:
An asterisk (*),
specifying list-directed I/O.
The label of a FORMAT
statement containing the format specification.
An integer variable that has been assigned the label
of a FORMAT statement.
A character expression that provides the format
specification.
If the optional keyword FMT=is omitted, format must
be the second item in io-specifier-list.
 |
 |  |
 |
 | NOTE: The NML=
and FMT= specifier
may not both appear in the same io-specifier-list. |
 |
 |  |
 |
- [NML=]name
specifies the name of a namelist group for namelist-directed
input. name must have been defined in
a NAMELIST statement.
If the optional keyword NML=
is omitted, name must be the second item
in the list. The first item must be the unit specifier without the
optional keyword UNIT=.
The NML=
and FMT= specifier
may not both appear in the same io-specifier-list.
- ADVANCE=character-expression
specifies whether to use advancing I/O
for this statement. character-expression
can be one of the following arguments:
If the ADVANCE=
specifier appears in io-specifier-list,
unit must be connected to an external
file opened for formatted sequential I/O. Also, ADVANCE='NO'
must be specified if the EOR=
or SIZE= specifier
appear in the list. Nonadvancing I/O is incompatible with
list-directed and namelist I/O.
- END=stmt-label
specifies the label of the executable statement
to which control passes if an end-of-file record is encountered.
This specifier is only valid for reading files opened for sequential
access.
- EOR=stmt-label
specifies the label of the executable statement
to which control passes if an end-of-record condition is encountered.
This specifier may appear in io-specifier-list
only if ADVANCE='NO'
also appears in the list.
- IOSTAT=integer-variable
returns the I/O status after the statement
executes. If the statement successfully executes, integer-variable
is set to zero. If an end-of-file record is encountered without
an error condition, it is set to a negative integer. If an error
occurs, integer-variable is set to a
positive integer that indicates which error occurred.
- REC=integer-expression
specifies the number of the record to be read from
a file connected for direct access. This specifier cannot appear
in io-specifier-list with the NML=,
ADVANCE=, SIZE=,
and EOR= specifiers,
nor with FMT=*
(for list-directed I/O).
- SIZE=integer-variable
returns the number of characters that have been
read by this READ
statement. This specifier may appear in io-specifier-list
only if ADVANCE='NO'
also appears in the list.
- input-list
is a comma-separated list of data items for input.
The data items can include variables and implied-DO
lists.
- format
is one of the following:
An asterisk (*),
specifying list-directed I/O.
The label of a FORMAT
statement containing the format specification.
An integer variable that has been assigned the label
of a FORMAT statement.
An embedded format specification.
- name
is the name of a namelist group, as previously defined
by a NAMELIST
statement. Using the namelist-directed syntax, the READ
statement takes its input from standard input. To read from a connected
file, you must use the NML=
specifier with the full syntax form, as described below.
Description
The READ
statement transfers data from an external or internal file to internal
storage. An external file can be opened for sequential access or
direct access. If it is opened for sequential access, the READ
statement can perform the following types of I/O:
If the file is opened for direct access, the READ
statement can perform formatted or unformatted I/O.
READ statements
operating on internal files can perform formatted or list-directed
I/O.
Examples
The following examples illustrate different uses of the READ
statement.
Formatted sequential I/O
The following READ
statement reads 10 formatted records from a file opened for sequential
access, using an implied-DO
list to read the data into the array x_array.
If the end-of-file record is encountered before the array is filled,
execution control passes to the statement at label 99.
READ (41, "(F10.2)", END=99) (x_array(i),i=1,10) |
Nonadvancing I/O
The following READ
statement takes its input from a file that was opened for sequential
access and is connected to unit 9. It uses nonadvancing I/O
to read an integer into the variable key.
If the statement encounters the end-of-record condition before it
can complete execution, control will pass to the executable statement
at label 100.
After the statement executes, the number of characters that have
been read will be stored in cnt.
INTEGER :: key READ (UNIT=9, "(I4)", ADVANCE="NO", SIZE=cnt, EOR=100) key |
Internal file
The following statement inputs a string of characters from
the internal file cfile,
uses an embedded format specification to perform format conversion,
and stores the results in the variables i
and x:
READ (cfile, FMT="(I5, F10.5)") i, x |
Namelist-directed I/O
Each of the four READ
statements in the next example uses a different style of syntax
to do exactly the same thing:
NAMELIST /nl/ a, b, c READ (UNIT=5, NML=nl) ! 5 = standard input READ (5, nl) READ (*, NML=nl) ! * = standard input READ nl ! assume standard input |
List-directed I/O
The following statement takes its data from standard input,
storing the converted value in int_var.
The format conversion is based on the type of int_var.
If you knew the format, you could substitute for the asterisk
one of the following:
The label of the FORMAT
statement with the format specification, as in the following:
READ 100, int_var 100 FORMAT(I4) |
An embedded format specification, as in the following:
Unformatted direct-access I/O
The following statement takes its input from the file connected
to unit 31. The REC=
specifier indicates that the file has been opened for direct access
and that this statement will read the record whose number is stored
in the variable rec_num.
If an I/O error occurs during the execution of the statement,
an error number will be stored in ios,
and execution control will branch to the executable statement at
label 99.
READ (31, REC=rec_num, ERR=99, IOSTAT=ios) a, b |
Related statements
CLOSE,
OPEN, and WRITE.
Related concepts
For more about I/O concepts, including information
about files and different types of I/O, see Chapter 8 “I/O and file handling”. This chapter
also lists example programs that use I/O. For information
about I/O formatting, see Chapter 9 “I/O formatting”.