Intel® Fortran Compiler 16.0 User and Reference Guide
The character count edit descriptor returns the remaining number of characters in the current input record.
The corresponding I/O list item must be of type integer or logical. For example, suppose the following statements are specified:
READ (4,1000) XRAY, KK, NCHRS, (ICHR(I), I=1,NCHRS) 1000 FORMAT (E15.7,I4,Q,(80A1))
Two fields are read into variables XRAY and KK. The number of characters remaining in the record is stored in NCHRS, and exactly that many characters are read into the array ICHR. (This instruction can fail if the record is longer than 80 characters.)
If you place the character count descriptor first in a format specification, you can determine the length of an input record.
On output, the character count edit descriptor causes the corresponding I/O list item to be skipped.
Consider the following:
CHARACTER ICHAR(80) READ (4, 1000) XRAY, K, NCHAR, (ICHAR(I), I= 1, NCHAR) 1000 FORMAT (E15.7, I4, Q, 80A1)
The preceding input statement reads the variables XRAY and K. The number of characters remaining in the record is NCHAR, specified by the Q edit descriptor. The array ICHAR is then filled by reading exactly the number of characters left in the record. (Note that this instruction will fail if NCHAR is greater than 80, the length of the array ICHAR.) By placing Q in the format specification, you can determine the actual length of an input record.
Note that the length returned by Q is the number of characters left in the record, not the number of reals or integers or other data types. The length returned by Q can be used immediately after it is read and can be used later in the same format statement or in a variable format expression. (See Variable Format Expressions.)
Assume the file Q.DAT contains:
1234.567Hello, Q Edit
The following program reads in the number REAL1, determines the characters left in the record, and reads those into STR:
CHARACTER STR(80) INTEGER LENGTH REAL REAL1 OPEN (UNIT = 10, FILE = 'Q.DAT') 100 FORMAT (F8.3, Q, 80A1) READ (10, 100) REAL1, LENGTH, (STR(I), I=1, LENGTH) WRITE(*,'(F8.3,2X,I2,2X,<LENGTH>A1)') REAL1, LENGTH, (STR(I), & & I= 1, LENGTH) END
The output on the screen is:
1234.567 13 Hello, Q Edit
A READ statement that contains only a Q edit descriptor advances the file to the next record. For example, consider that Q.DAT contains the following data:
abcdefg abcd
Consider it is then READ with the following statements:
OPEN (10, FILE = "Q.DAT") READ(10, 100) LENGTH 100 FORMAT(Q) WRITE(*,'(I2)') LENGTH READ(10, 100) LENGTH WRITE(*,'(I2)') LENGTH END
The output to the screen would be:
7 4