READ Statement (Executable) [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Reference
READ Statement (Executable)
The READ statement transfers data from a file to program variables.
There are two kinds of READ statements:
* Standard input READ
* File READ
The standard input READ statement complements the PRINT statement. The
file READ statement complements the WRITE statement. A more detailed
description of the READ statement is found in Chapter 4 .
Standard Input READ Statement (Executable)
The standard input READ statement transfers data to memory from a unit
that is designated as the standard input unit. (FORTRAN unit 5 is
preconnected to the system standard input, usually the user's terminal.)
------------------------------------------------------------------------------------------------
| | | |
| Item | Description/Default | Restrictions |
| | | |
------------------------------------------------------------------------------------------------
| | | |
| fmt | Format designator. | See "Semantics". |
| | | |
------------------------------------------------------------------------------------------------
| | | |
| namelist_group_name | Symbolic name specifying a list of | None. |
| | variables or arrays previously | |
| | declared in a NAMELIST statement. | |
| | | |
------------------------------------------------------------------------------------------------
| | | |
| implied_do_list | An implied DO loop. Refer to "DO | None. |
| | Statement (Executable)". | |
| | | |
------------------------------------------------------------------------------------------------
Semantics
The format designator must be one of the following:
* The statement label of a FORMAT statement.
* An INTEGER*4 variable to which the statement label of a FORMAT
statement has been assigned by an ASSIGN statement.
* A character or noncharacter array name that contains the
representation of a format descriptor list enclosed in
parentheses. The use of a noncharacter array is an extension to
the ANSI 77 standard.
* A character expression that evaluates to the representation of a
format descriptor list enclosed in parentheses.
* An asterisk, which specifies list-directed output. See
"List-Directed Input/Output" for details.
Each item in the variable list specifying where the data is to be
transferred must be one of the following:
* A variable name.
* An array element name.
* An array name.
* A substring.
* An implied DO loop containing the above items only.
* A scalar record element name.
Examples Notes
---------------------------------------------------------------------------------------
READ 10,num,des Reads the values of num and des according
to FORMAT statement 10.
READ *,a,b,n Reads the values of a, b, and n according
to list-directed formatting.
ASSIGN 100 TO fmt Reads the values of al and h1 according to
READ fmt,al,h1 FORMAT statement 100.
READ '(3I3)',i,j,k Reads the values of i, j, and k according
to the format specification in the READ
statement itself.
READ 5 Skips a record on the standard input
device.
File READ Statement (Executable)
The file READ statement transfers data from a file to memory.
----------------------------------------------------------------------------------------------
| | | |
| Item | Description/Default | Restrictions |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| unit | Arithmetic expression of | See "Semantics". |
| | type integer. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| address | Expression specifying unit | Must be an integer: zero or |
| | number of a sequential file. | positive. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| char_variable | Internal file from which | Character variable or scalar |
| | input is taken. | record field. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| char_array_element | Internal file from which | Character array element or |
| | input is taken. | scalar record field. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| char_substring | Internal file from which | Character substring. |
| | input is taken. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| integer_expression | Expression specifying the | Must be an integer: zero or |
| | unit number of a sequential | positive. |
| | file. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| * | Asterisk indicates that the | None. |
| | standard input device (unit | |
| | 5, usually the terminal) is | |
| | to be used. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| fmt | Format designator. | fmt must be as specified for |
| | | a standard input READ above. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| namelist_group_name | Symbolic name specifying a | Cannot appear in a statement |
| | list of variables or arrays | containing a format |
| | previously declared in a | specifier. |
| | NAMELIST statement. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| ios | Integer variable or integer | Must be an integer type. |
| | array element name for error | |
| | return. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| label | Statement label of an | Must be the label of a |
| | executable statement. | statement in the same |
| | | program unit. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| rec | Specifies the record number | If fmt is an asterisk, a |
| | in a direct access file. | record specifier must not be |
| | | present. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| zbf | Variable, array name, or | Extension to ANSI 77 |
| | array element name. | standard; cannot be a |
| | | character type. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| zln | Integer expression used with | Extension to ANSI 77 |
| | ZLEN. | standard. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| end | Statement label of an | None. |
| | executable statement. | |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| key_value | See "Semantics". | Integer variable, character |
| | | expression, or scalar record |
| | | field. |
| | | |
----------------------------------------------------------------------------------------------
| | | |
| key_num | See "Semantics". | None. |
| | | |
----------------------------------------------------------------------------------------------
Semantics
A file READ statement must contain a unit specifier and at most one of
each of the other specifiers.
If the prefix UNIT= is omitted, unit must be the first item in the list.
This is the unit number for the input device or file.
If the prefix FMT= is omitted, fmt must be the second item in the list
and unit (without a prefix) must be the first item.
If fmt is omitted and no NML specifier is present, the access is
unformatted (binary). record_name and aggregate variables can only be
used in unformatted reads.
If a record number is specified, the unit must be connected for direct
access. You can specify a record number through the REC specifier. Note
that REC cannot appear with the END or NML specifiers nor with the FMT=*
form of the FMT specifier. You can also specify a record number with the
@ specifier.
If the ERR specifier is present and an error occurs during execution of
the READ statement, control transfers to the specified statement rather
than aborting the program.
If the IOSTAT specifier is present and an error occurs, the error code is
returned in the IOSTAT variable and the program is not aborted. Refer to
Appendix A for the IOSTAT error codes.
If the END specifier is present and an end-of-file is encountered in a
sequential file during the execution of the READ statement, control
transfers to the specified statement. In this case, ios is set to -1.
The ZBUF and ZLEN specifiers and the address alternative used as a
parameter for the UNIT specifier are extensions to the ANSI 77 standard,
and are included for compatibility with programs originally written in
another version of FORTRAN. If used in a program, their syntax is
checked, but they are otherwise ignored by the compiler.
As an extension to the ANSI 77 standard, indexed sequential access (ISAM)
is allowed with a READ statement. The following specifiers are used to
establish the desired match criterion to read a record from an indexed
file:
KEY = key_value
KEYEQ = key_value
KEYGT = key_value
KEYLT = key_value
where key_value is an integer value or character expression. Any one of
the specifiers can appear in a READ statement. The specifiers can be
omitted. If a specifier is not present, the primary key is assumed if it
is the first read of the file. Otherwise, the file is read sequentially
from the last position of the previous read.
If the KEYID specifier is not present, the primary key is assumed. The
subsequent reads do not assume the previous KEYID value for the current
read.
As an extension to the ANSI 77 standard, sequential reads (without the
REC specifier) are allowed on files open for direct access. If the REC
specifier is omitted, a READ statement reads the next record.
Examples Notes
---------------------------------------------------------------------------------------
READ (8,10)a,b,c Reads the values of a, b, and c from the
file connected to unit 8 according to
FORMAT statement 10.
ASSIGN 4 TO num Reads the value of z from the file
READ (UNIT=3,ERR=50,FMT=num) connected to unit 3 according to FORMAT
statement 4. If an error occurs, control
transfers to statement 50.
READ (10)x Reads the value of x from the file
connected to unit 10. Because fmt is
omitted, the data is unformatted.
READ (10,FMT=*,END=60)b Reads the value of b from the file
connected to unit 10, according to
list-directed formatting. If an
end-of-file is encountered, control passes
to statement 60.
READ (2,'(I3)',REC=10)i Reads the value of i from the 10th record
of the direct access file connected to unit
2, according to the format specification in
the READ statement itself.
READ (10) Skips a record in the file connected to
unit 10.
CHARACTER*8 a Reads the value of b from the character
REAL b variable a according to the format
a = ' $27.97' specification in the READ statement itself.
READ (a(4:8),'(F5.2)') b
READ (10,KEYID=0,KEYEQ='100',ERR=101)buf Reads an ISAM record with primary key value
'100' into buf.
MPE/iX 5.0 Documentation