SuivantPrec.Bas prec.BasNiv. sup.

10.16 READ and WRITE Statements 

The READ statement reads information from one or more records on a file into a list of variables, array elements, etc. The WRITE statement writes information from a list of items which may include variables, arrays, and expressions and produces one or more records on a file. Each READ or WRITE statement can only transfer one record on an unformatted file but on formatted files, including internal files, more than one record may be transferred, depending on the contents of the format specification.

The two statements have the same general form:       READ( control-list ) data-list       WRITE( control-list ) data-list The control-list must contain a unit identifier; the other items may be optional depending on the type of file. The data-list is also optional: if it is absent the statement transfers one record (or possibly more under the control of a format specification).

Unit Identifier 

This may have any of the following forms:

UNIT= integer-expression The value of the expression must be zero or greater and must refer to a valid I/O unit.
UNIT=* For the standard pre-connected input or output file.
UNIT=internal-file The internal-file may be a variable, array-element, substring, or array of type character, see section 10.3.
Note that the keyword UNIT= is optional if the unit identifier is the first item in the control list.

Format Identifier 

A format identifier must be provided when using a formatted (or internal) file but not otherwise. It may have any of the following forms:

FMT=label The label of a FORMAT statement in the same program unit.
FMT=format The format may be a character expression or character array containing a complete format specification (section 10.6).
FMT=* For list-directed formatting (section 10.10).
Note that the keyword FMT= is also optional if the format identifier is the second item in the control list and the first item is a unit identifier specified without its keyword.

Record Number 

A record number identifier must be provided for direct-access files but not otherwise. It has the form:

REC=integer-expression

The record number must be greater than zero; for READ it must refer to a record which exists.

Error and End-of-file Identifiers 

These may be provided in any combination, but END=label is only valid when reading a sequential or internal file. See 10.5 for more information.
 END=label
 ERR=label
 IOSTAT=integer-variable
The data list of a READ statement may contain variables, array-elements, character-substrings, or complete arrays of any data type. An array-name without subscripts represents all the elements of the array; this is not permitted for assumed-size dummy arguments in procedures (because the array size is indeterminate). The list may also contain implied DO-loops (explained below).

The data list of a WRITE statement may contain any of the items permitted in a READ statement and in addition expressions of any data type. As in all I/O statements, expressions must not themselves involve the execution of other I/O statements.

Implied DO-loops 

The simplest and most efficient way to read or write all the elements of an array is to put its name, unsubscripted, in the data-transfer list. In the case of a multi-dimensional array the elements will be transferred in the normal storage sequence, with the first subscript varying most rapidly.

An implied-DO loop allows the elements to be transferred selectively or in some non-standard order. The rules for an implied-DO are similar to that of an ordinary DO-loop but the loop forms a single item in the data-transfer list and is enclosed by a pair of parentheses rather than by DO and CONTINUE statements. For example:
       READ(UNIT=*, FMT=*) (ARRAY(I), I= IMIN, IMAX)
       WRITE(UNIT=*, FMT=15) (M, X(M), Y(M), M=1,101,5)
 15    FORMAT(1X, I6, 2F12.3)
A multi-dimensional array can be printed out in a transposed form. The next example outputs an array X(100,5) but with 5 elements across and 100 lines vertically:
       WRITE(UNIT=*, FMT=5) (I,I=1,5),
      $                     ((L,X(L,I),I=1,5),L=1,100)
 5     FORMAT(1X,'LINE', 5I10, 100(/,1X,I4, 5F10.2))
The first loop writes headings for the five columns, then the double loop writes a line-number for each line followed by five array elements. Note that the parentheses have to be matched and that a comma is needed after the inner right-parenthesis since the inner loop is just an item in the list contained in the outer loop.

The implied DO-loop has the general form:       ( data-list, loop-variable = start, limit, step ) where the rules for the start, limit, and step values are exactly as in an ordinary DO statement. The loop-variable (normally an integer) may be used within the data-list and this list may, in turn, include further complete implied-DO lists.

If an error or end-of-file condition occurs in an implied DO-loop then the loop-control variable will be undefined on exit; this means that an explicit DO-loop is required to read an indefinite list of data records and exit with knowledge of how many items were actually input.

SuivantPrec.Bas prec.HautNiv. sup.