8.10 Format Control Interaction with I/O Lists

Format control begins with the execution of a formatted I/O statement. The action taken by format control depends on information provided jointly by the next element of the I/O list (if one exists) and the next field descriptor of the format specification. Both the I/O list and the format specification are interpreted from left to right, except when repeat counts and implied-DO lists are specified.

If the I/O statement contains an I/O list, you must specify at least one field descriptor (I, O, Z, F, E, D, G, L, or A) or the edit descriptor Q in the format specification or an error will occur.

On execution, a formatted input statement reads one record from the specified unit and initiates format control. Thereafter, additional records are read as indicated by the format specification. Format control requires that a new record be read when a slash occurs in the format specification, or when the last closing parenthesis of the format specification is reached and I/O list elements remain to be filled. Any remaining characters in the current record are discarded when the new record is read.

On execution, a formatted output statement transmits a record to the specified unit as format control terminates. Records can also be written during format control if a slash appears in the format specification or if the last closing parenthesis is reached and more I/O list elements remain to be transferred.

The field descriptors I, O, Z, F, E, D, G, L and A, and the edit descriptor Q each correspond to one element in the I/O list. No list element corresponds to an H, X, P, T, TL, TR, SP, SS, S, BN, BZ, $, :, or character constant edit descriptor. In H and character constant edit descriptors, data transfer occurs directly between the external record and the format specification.

When an I/O list element is to be transferred, format field descriptors are processed, beginning with the current format item, until a descriptor is found that corresponds to an I/O list element. The I/O list element is then transferred under control of the field descriptor.

Format execution continues until one of the following is encountered: an element-transferring field descriptor, a colon edit descriptor, or the end of the format. These also terminate format execution when no I/O list elements are to be transferred.

When the last closing parenthesis of the format specification is reached, format control determines whether more I/O list elements are to be processed. If not, format control terminates. However, if additional list elements remain, part or all of the format specification is reused in a process called format reversion.

In format reversion, the current record is terminated, a new one is initiated, and format control reverts to the group repeat specification whose opening parenthesis matches the next-to-last closing parenthesis of the format specification. If the format does not contain a group repeat specification, format control returns to the initial opening parenthesis of the format specification. Format control continues from that point.

Example

Assume that the data in a file is to be processed 2 records at a time. Each record starts with a number to be put into an element of a vector B, followed by 5 numbers to be put in a row in matrix A.

FOR002.DAT contains the following data:

001 0101 0102 0103 0104 0105
002 0201 0202 0203 0204 0205
003 0301 0302 0303 0304 0305
004 0401 0402 0403 0404 0405
005 0501 0502 0503 0504 0505
006 0601 0602 0603 0604 0605
007 0701 0702 0703 0704 0705
008 0801 0802 0803 0804 0805
009 0901 0902 0903 0904 0905
010 1001 1002 1003 1004 1005

Example 8-1 shows how several different format specifications interact with I/O lists to process data in file FOR002.DAT.

Example 8-1 Interaction Between FORMAT Statements and I/O Lists

        INTEGER I, J, A(2,5), B(2)

        OPEN (unit=2, access='sequential', file='FOR002.DAT')

        READ (2,100) (B(I), (A(I,J), J=1,5),I=1,2) 
   100  FORMAT (2 (I3, X, 5(I4,X), /) ) 

        WRITE (6,999) B, ((A(I,J),J=1,5),I=1,2) 
   999  FORMAT (' B is ', 2(I3, X), ';  A is', /
       1       (' ', 5 (I4, X)) )

        READ (2,200) (B(I), (A(I,J), J=1,5),I=1,2) 
   200  FORMAT (2 (I3, X, 5(I4,X), :/) )

        WRITE (6,999) B, ((A(I,J),J=1,5),I=1,2) 

        READ (2,300) (B(I), (A(I,J), J=1,5),I=1,2) 
   300  FORMAT ( (I3, X, 5(I4,X)) )

        WRITE (6,999) B, ((A(I,J),J=1,5),I=1,2) 

        READ (2,400) (B(I), (A(I,J), J=1,5),I=1,2) 
   400  FORMAT ( I3, X, 5(I4,X) )

        WRITE (6,999) B, ((A(I,J),J=1,5),I=1,2) 

        END

This statement starts by reading B(1); then A(1,1) through A(1,5); then B(2) and A(2,1) through A(2,5).

The first record (starting with 001) is read to start the processing of the I/O list.

There are two records, each in the format I3, X, 5(I4, X). The slash (/) forces the reading of the second record after A(1,5) is processed; it also forces the reading of the third record after A(2,5) is processed-no data is taken from that record.

This statement produces the following output:

B is   1   2 ;  A is
 101  102  103  104  105
 201  202  203  204  205

This statement reads the record starting with 004. The slash (/) forces the reading of the next record after A(1,5) is processed; the colon (:) stops the reading after A(2,5) is processed but before the slash (/) forces another read.

This statement produces the following output:

B is   4   5 ;  A is
 401  402  403  404  405
 501  502  503  504  505

This statement reads the record starting with 006. After A(1,5) is processed, format reversion reads the next record and starts format processing at the parenthesis (() before the I3.

This statement produces the following output:

B is   6   7 ;  A is
 601  602  603  604  605
 701  702  703  704  705

This statement reads the record starting with 008. After A(1,5) is processed, format reversion reads the next record and starts format processing at the parenthesis ( ( ) before the I4.

This statement produces the following output:

B is   8  90 ;  A is
 801  802  803  804  805
 9010 9020 9030 9040 100

The record 009 0901 0902 0903 0904 0905 is processed with I4 as "009 " for B(2), which is 90; X skips the next "0"; then "901 " is processed for A(2,1), which is 9010; "902 " for A(2,2); "903 " for A(2,3); and "904 " for A(2,4). The repetition factor of 5 is now exhausted and the format ends. Format reversion reads another record and starts format processing at the parenthesis ( ( ) before the I4 so that "010 " is read for A(2,5), which is 100.


Previous Page Next Page Table of Contents