A format specification can appear in a FORMAT statement or character expression. In a FORMAT statement, it is preceded by the keyword FORMAT. A format specification takes the following form:
|Data edit descriptors:||I, B, O, Z, F, E, EN, ES, D, G, L, and A.|
|Control edit descriptors:||T, TL, TR, X, S, SP, SS, BN, BZ, P, :, /, $, \, and Q.|
|String edit descriptors:||H, 'c', and "c", where c is a character constant.|
A comma can be omitted in the following cases:
Edit descriptors can be nested and a repeat specification can precede data edit descriptors, the slash edit descriptor, or a parenthesized list of edit descriptors.
Rules and Behavior
A FORMAT statement must be labeled.
Named constants are not permitted in format specifications.
If the associated I/O statement contains an I/O list, the format specification must contain at least one data edit descriptor or the control edit descriptor Q.
Blank characters can precede the initial left parenthesis, and additional blanks can appear anywhere within the format specification. These blanks have no meaning unless they are within a character string edit descriptor.
When a formatted input statement is executed, the setting of the BLANK specifier (for the relevant logical unit) determines the interpretation of blanks within the specification. If the BN or BZ edit descriptors are specified for a formatted input statement, they supersede the default interpretation of blanks. (For more information on BLANK defaults, see Section 12.6.4 for details on OpenVMS systems, or Section 13.6.4 for details on Tru64 UNIX, Linux, and Windows systems.)
For formatted input, use the comma as an external field separator. The comma terminates the input of fields (for noncharacter data types) that are shorter than the number of characters expected. It can also designate null (zero-length) fields.
The first character of a record transmitted to a line printer or terminal is typically used for carriage control; it is not printed. The first character of such a record should be a blank, 0, 1, $, +, or ASCII NUL. Any other character is treated as a blank.
A format specification cannot specify more output characters than the external record can contain. For example, a line printer record cannot contain more than 133 characters, including the carriage control character.
Table 11-1 summarizes the edit descriptors that can be used in format specifications.
Table 11-1 Summary of Edit Descriptors
|A||A[w]||Transfers character or Hollerith values.||(Section 11.2.6)|
|B||Bw[.m]||Transfers binary values.||(Section 184.108.40.206)|
|BN||BN||Ignores embedded and trailing blanks in a numeric input field.||(Section 220.127.116.11)|
|BZ||BZ||Treats embedded and trailing blanks in a numeric input field as zeros.||(Section 18.104.22.168)|
|D||Dw.d||Transfers real values with D exponents.||(Section 22.214.171.124)|
|E||Ew.d[Ee]||Transfers real values with E exponents.||(Section 126.96.36.199)|
|EN||ENw.d[Ee]||Transfers real values with engineering notation.||(Section 188.8.131.52)|
|ES||ESw.d[Ee]||Transfers real values with scientific notation.||(Section 184.108.40.206)|
|F||Fw.d||Transfers real values with no exponent.||(Section 220.127.116.11)|
|G||Gw.d[Ee]||Transfers values of all intrinsic types.||(Section 18.104.22.168)|
|H||nHch[ch...]||Transfers characters following the H edit descriptor to an output record.||(Section 11.4)|
|I||Iw[.m]||Transfers decimal integer values.||(Section 22.214.171.124)|
|L||Lw||Transfers logical values: on input, transfers characters; on output, transfers T or F.||(Section 11.2.5)|
|O||Ow[.m]||Transfers octal values.||(Section 126.96.36.199)|
|P||kP||Interprets certain real numbers with a specified scale factor.||(Section 11.3.5)|
|Q||Q||Returns the number of characters remaining in an input record.||(Section 11.3.9)|
|S||S||Reinvokes optional plus sign (+) in numeric output fields; counters the action of SP and SS.||(Section 188.8.131.52)|
|SP||SP||Writes optional plus sign (+) into numeric output fields.||(Section 184.108.40.206)|
|SS||SS||Suppresses optional plus sign (+) in numeric output fields.||(Section 220.127.116.11)|
|T||Tn||Tabs to specified position.||(Section 18.104.22.168)|
|TL||TLn||Tabs left the specified number of positions.||(Section 22.214.171.124)|
|TR||TRn||Tabs right the specified number of positions.||(Section 126.96.36.199)|
|X||nX||Skips the specified number of positions.||(Section 188.8.131.52)|
|Z||Zw[.m]||Transfers hexadecimal values.||(Section 184.108.40.206)|
|$||$||Suppresses trailing carriage return during interactive I/O.||(Section 11.3.8)|
|:||:||Terminates format control if there are no more items in the I/O list.||(Section 11.3.7)|
|/||[r]/||Terminates the current record and moves to the next record.||(Section 11.3.6)|
|\||\||Continues the same record; same as $.||(Section 11.3.8)|
|'c'1||'c'||Transfers the character literal constant (between the delimiters) to an output record.||(Section 11.4)|
|1 These delimiters can also be quotation marks (").|
Character Format Specifications
In data transfer I/O statements, a format specifier ([FMT=]format) can be a character expression that is a character array, character array element, or character constant. This type of format is also called a run-time format because it can be constructed or altered during program execution.
The expression must evaluate to a character string whose leading part is a valid format specification (including the enclosing parentheses).
If the expression is a character array element, the format specification must be contained entirely within that element.
If the expression is a character array, the format specification can continue past the first element into subsequent consecutive elements.
If the expression is a character constant delimited by apostrophes,
use two consecutive apostrophes (
'' ) to represent an
apostrophe character in the format specification; for example:
PRINT '("NUM can''t be a real number")'
Similarly, if the expression is a character constant delimited by quotation marks, use two consecutive quotation marks ("") to represent a quotation mark character in the format specification.
To avoid using consecutive apostrophes or quotation marks, you can put the character constant in an I/O list instead of a format specification, as follows:
The following shows another character format specification:
PRINT "(A)", "NUM can't be a real number"
In the following example, the format specification changes with each iteration of the DO loop:
WRITE (6, '(I12, I4, I12)') I, J, K
The DATA statement assigns a left parenthesis to character array element FORCHR(0), and (for later use) a right parenthesis and three F edit descriptors to character variables.
SUBROUTINE PRINT(TABLE) REAL TABLE(10,5) CHARACTER*5 FORCHR(0:5), RPAR*1, FBIG, FMED, FSML DATA FORCHR(0),RPAR /'(',')'/ DATA FBIG,FMED,FSML /'F8.2,','F9.4,','F9.6,'/ DO I=1,10 DO J=1,5 IF (TABLE(I,J) .GE. 100.) THEN FORCHR(J) = FBIG ELSE IF (TABLE(I,J) .GT. 0.1) THEN FORCHR(J) = FMED ELSE FORCHR(J) = FSML END IF END DO FORCHR(5)(5:5) = RPAR WRITE (6,FORCHR) (TABLE(I,J), J=1,5) END DO END
Next, the proper F edit descriptors are selected for inclusion in the format specification. The selection is based on the magnitude of the individual elements of array TABLE.
A right parenthesis is added to the format specification just before the WRITE statement uses it.
For More Information: