Many edit descriptors discussed in previous pages require values. More precisely, edit descriptors I, F, E, L and A either take the values of variables and print them, or read in values into variables. On the other hand, edit descriptors nX, Tc, TLc and TRc do not require any value and are used mainly for positioning. Do the rules discussed in Scanning a Format: I apply to these edit descriptors? The answer is yes; but, an extension is required. In fact, this extension is very simple, although it is easily overlooked. Here is the rule:
Once a READ or WRITE, using format, starts execution, Fortran starts scanning the format and carries out what the edit descriptors say until encounters an edit descriptor that requires a value. Then, that value is read or printed using the encountered edit descriptor and format scanning continues until encounters an edit descriptor that requires a value. This process continues until all data items are processed or reaches the end of format.
The best way to explain the above rule is using several examples.
Suppose we have the following input lines:INTEGER :: a, b READ(*,"(T2,I4,T10,I5,T18,I2)") a, b
Once the READ statement starts, Fortran starts scanning the format. It sees T2, which means moving to position 2. The next one is I4. Since I4 requires a value, Fortran takes the content in positions 2 to 5 and converts it to an integer. Thus, a receives 2345. After finishing this, Fortran scans for the next edit descriptor which requires a value. In this process, Fortran encounters T10, which means moving to position 10, and then sees I5. Therefore, the content in positions 10 to 14 is converted to an integer 1234 and stored in b. After this, Fortran scans the format again and sees T18, which means moving to position 18, followed by I2. Thus, the next position for reading is 18 and the next edit descriptor is I2. However, since all data items have received values, format scanning stops at position 18 and the execution of the READ completes.....5....0....5....0 12345678901234567890
Suppose we have the following input lines:INTEGER :: a, b, c READ(*,"(T2,I4,T10,I5)") a, b, c
When the READ starts, format scanning begins at T2 which means moving to position 2 and then encounters I4. Since this edit descriptor requires a value, it takes the content in positions 2 to 5, converts to an integer 2345 and stores in a. Then, format scan continues and sees T10 followed by I5. Thus, the content in positions 10 to 14 is taken, converted to 1234 and stored in b. However, this approaches the end of the format. Therefore, Fortran skips to the next line and rescans the format. When rescanning starts, Fortran sees T2 followed by I4. Thus, the content in positions 2 to 5 on the second input line is taken, converted to an integer 9876, and stored in c. Then, Fortran proceeds and sees T10 followed by I5. However, since there is no variable in the READ statement, this READ completes. Note that when this READ completes, the next available position is position 10 on the second input line.....5....0....5....0 12345678901234567890 09876543210987654321
Format scanning starts with 1X, which skips the first position, and then encounters I5 which requires a value. Then, Fortran prints the next variable (i.e., a) with I5. Therefore, positions 2 to 6 contain two spaces followed by 1, 2 and 3. Format scanning continues and sees T10, which means moving to position 10, followed by I5. Hence, the value of b is printed in positions 10 to 14. Now, since all variables are printed and the format ends there, the WRITE statement completes and the the output isINTEGER :: a = 123, b = 789 WRITE(*,"(1X,I5,T10,I5)") a, b
....5....0....5 123 789 ^^^^^ ^^^^^ I5 I5
The way of scanning format is the same as the above after b is printed. Since c is not printed, format scanning continues and encounters the closing right parenthesis. This causes advancing to the next line and continues search for an edit descriptor that requires a value. In this search, we see 1X, followed by I5, and the result up to now isINTEGER :: a = 123, b = 789, c = 456 WRITE(*,"(1X,I5,T10,I5)") a, b, c
Format scanning continues with T10 and then I5. However, since all variables have been printed, the WRITE statement completes. At this point, the next available position is position 10 on the second output line.....5....0....5 123 789 456