# Interaction Between Format Specifications and I/O Lists

Format control begins with the execution of a formatted I/O statement. Each action of format control depends on information provided jointly by the next item in the I/O list (if one exists) and the next edit descriptor in the format specification.

Both the I/O list and the format specification are interpreted from left to right, unless repeat specifications or implied-DO lists appear.

If an I/O list specifies at least one list item, at least one data edit descriptor (I, B, O, Z, F, E, EN, ES, EX, D, G, L, or A) or the Q edit descriptor must appear in the format specification; otherwise, an error occurs.

Each data edit descriptor (or Q edit descriptor) corresponds to one item in the I/O list, except that an I/O list item of type complex requires the interpretation of two F, E, EN, ES, EX, D, or G edit descriptors. No I/O list item corresponds to a control edit descriptor (X, P, T, TL, TR, SP, SS, S, BN, BZ, $, or :), or a character string edit descriptor (H and character constants). For character string edit descriptors, data transfer occurs directly between the external record and the format specification.

When format control encounters a data edit descriptor in a format specification, it determines whether there is a corresponding I/O list item specified. If there is such an item, it is transferred under control of the edit descriptor, and then format control proceeds. If there is no corresponding I/O list item, format control terminates.

If there are no other I/O list items to be processed, format control also terminates when the following occurs:

- A colon edit descriptor is encountered.

- The end of the format specification is reached.

If additional I/O list items remain, part or all of the format specification is reused in format reversion.

In format reversion, the current record is terminated and a new one is initiated. Format control then reverts to one of the following (in order) and continues from that point:

1. The group repeat specification whose opening parenthesis matches the next-to-last closing parenthesis of the format specification

2. The initial opening parenthesis of the format specification

Format reversion has no effect on the scale factor (P), the sign control edit descriptors (S, SP, or SS), or the blank interpretation edit descriptors (BN or BZ).

## Examples

The data in file FOR002.DAT 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

The following example shows how several different format specifications interact with I/O lists to process data in file FOR002.DAT:

            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 reads B(1); then A(1,1) through A(1,5); then B(2) and A(2,1) through A(2,5).

The first record read (starting with 001) starts 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 causes the next record to be read and starts format processing at the left 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 causes the next record to be read and starts format processing at the left 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 repeat specification of 5 is now exhausted and the format ends. Format reversion causes another record to be read and starts format processing at the left parenthesis before the I4, so "010 " is read for A(2,5), which is 100.

Parent topic: I/O Formatting

## See Also

Data edit descriptors
Control edit descriptors
Q edit descriptor
Character string edit descriptors
Scale Factor Editing (P)