READ (Sequential) - Fortran

The sequentialREAD statement transfers data from an external record to the items specified by the input list. Transfers occur using the sequential-access method or keyed-access method.

The four forms of the sequential READ statement are

  • formatted
  • list-directed
  • unformatted
  • namelist-directed

The following parameters apply to all four forms of the sequential READ statement:

  • [UNIT=]unum
  • is a unit or internal file to be acted upon.
    f is a format specifier.
    * is a list-directed input specifier.
  • NML=[group-name ] is a namelist specifier. If the keyword NML is omitted, group-name must immediately follow unum.
  • IOSTAT=rn is the name of the variable in which I/O completion status is posted.
  • ERR=s is the statement label to which control is transferred after an error.
  • END=eof is the statement label to which control is transferred on end of file.

iolist specifies memory location where data is read.

Formatted READ (Sequential)

Syntax

READ ([UNIT=]unum,f[,IOSTAT=rn][,ERR=s] [,END=eof]) [iolist]
READ f[,iolist]

Method of Operation
A formatted READ statement transfers data from an external record to internal storage. It translates the data from character to binary format using the f specifier to edit the data.

List-Directed READ (Sequential)
Syntax

READ ([UNIT=]unum,*[,IOSTAT=rn][,ERR=s][,END=eof])[iolist]
READ f*[iolist]

Method of Operation
A list-directed READ statement transfers data from an external record to internal storage. It translates the data from character to binary format using the data types of the items in iolist to edit the data.

Rules for Use

  1. The external record can have one of the following values:
    • A constant with a data type of integer, real, logical, complex, or character. The rules given in previous section, “Constants and Data Structures,” define the acceptable formats for constants in the external record.
    • A null value, represented by a leading comma, two consecutive constants without intervening blanks, or a trailing comma.
    • A repetitive format n*constant, where n is a nonzero, unsigned integer constant indicating the number of occurrences of constant.n* represents repetition of a null value.
  2. Hollerith, octal, and hexadecimal constants are not allowed.
  3. A value separator must delimit each item in the external record; a value separator can be one of the following:
    • one or more spaces or tabs
    • a comma, optionally surrounded by spaces or tabs
  4. A space, tab, comma, or slash appearing within a character constant are processed as part of the constant, not as delimiters.
  5. A slash delimits the end of the record and causes processing of an input statement to halt; the slash can be optionally surrounded by spaces and/or tabs. Any remaining items in iolist are unchanged after the READ.
  6. When the external record specified contains character constants, a slash must be specified to terminate record processing. If the external record ends with a blank, the first character of the next record processed follows immediately after the last character of the previous record.
  7. Each READ statement reads as many records as is required by the specifications in iolist. Any items in a record appearing after a slash are ignored.

Unformatted READ (Sequential)
Syntax

READ ([UNIT=]unum[,IOSTAT=rn][,ERR=s][,END=eof]) [iolist]

Method of Operation
An unformatted READ statement transfers data from an external record to internal storage. The READ operation performs no translation on read-in data. The data is read in directly to the items in iolist. The type of each data item in the input record must match that declared for the corresponding item in iolist.

When a sequential-unformatted READ is performed on a direct-access file, the next record in the direct-access file is assumed.

Rules for Use

  • There must be at least as many items in the unformatted record as there are in iolist. Additional items in the record are ignored, and a subsequent READ accesses the next record in the file.
  • The type of each data item in the i3nput record must match the corresponding data item in iolist.

Namelist-Directed READ (Sequential)
Syntax

READ (unum,NML=group-name[,IOSTAT=rn][,ERR=s][,END=eof]) [iolist]
READ name

Method of Operation
A namelist-directed READ statement locates data in a file using the group name in a NAMELIST statement. It uses the data types of the items in the corresponding NAMELIST statement and the forms of the data to edit the data.

Figure below illustrates rules for namelist input data and shows its format.

Figure: Namelist Input Data Rules

Namelist Input Data Rules

Rules for Use

  • Both group-name and item must be contained within a single record.
  • Spaces and/or tabs are not allowed within group-name or item. However, item can contain spaces or tabs within the parentheses of a subscript or substring specifier.
  • The value item can be any of the values
  • A value separator must delimit each item in a list of constants.
  • A separator must delimit each list of value assignments.
  • When value contains character constants, a dollar sign ($) or ampersand (&) must be specified to terminate processing of the namelist input. If the namelist input ends with a blank, the first character of the next record processed follows immediately after the last character of the previous record.
  • Entering a question mark (?) after a namelist-directed READ statement is executed causes the group-name and current values of the namelist items for that group to be displayed.
  • You can assign input values in any order in the format item=value. Multiple-line assignment statements are allowed. Each new line must begin on or after column 2; column 1 is assumed to contain a carriage-control character. Any other character in column 1 is ignored.
  • You can assign input values for the following data types: integer, real, logical, complex, and character.
  • Numeric-to-character and character-to-numeric conversions are not allowed.
  • Constant values must be given for assigned values, array subscripts, and substring specifiers. Symbolic constants defined by a PARAMETER statement are not allowed.

Example
In the following example, the name of a file is read from the standard input into filename, the file is opened, and the first record is read. A branch is taken to statement 45 (not shown) when end of file is encountered.

read (*,10) filename
10 format (a)
open (2,file=filename)
read (2, 20, end=45) word
20 format (A50)

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Fortran Topics