Purpose |
FORTRAN 77 |
f77 Extensions |
---|---|---|
Blank control | BN, BZ | B |
Carriage control | /, space, 0, 1 | $ |
Character edit | nH, Aw, 'aaa' | "aaa", A |
Floating-point edit | Dw.dEe, Ew.dEe, Fw.dEe, Gw.dEe | Ew.d.e, Dw.d.e, Gw.d.e |
Hexadecimal edit | Zw.m | |
Integer edit | Iw.m | |
Logical edit | Lw | |
Octal edit | Ow.m | |
Position control | nX, Tn, TLn, TRn | nT, T, X |
Radix control | nR, R | |
Remaining characters | Q | |
Scale control | nP | P |
Sign control | S, SP, SS | SU |
Terminate a format | : | |
Variable format expression | < e > |
Specifiers can be uppercase as well as lowercase characters in format statements and in all the alphabetic arguments to the I/O library routines.
The definitions for the parameters, w, m, d, and e are:
w and e are non-zero, unsigned integer constants.
d and m are unsigned integer constants.
w specifies that the field occupies w positions.
m specifies the insertion of leading zeros to a width of m.
d specifies the number of digits to the right of the decimal point.
e specifies the width of the exponent field.
You can write field descriptors A, D, E, F, G, I, L, O, or Z without the w, d, or e field indicators. @ If these are left unspecified, the appropriate defaults are used based on the data type of the I/O list element. See Table 5-3.
Typical format field descriptor forms that use w, d, or e include:
Aw, Iw, Lw, Ow, Zw, Dw.d, Ew.d, Gw.d, Ew.dEe, Gw.dEe
Example: With the default w=7 for INTEGER*2, and since 161 decimal = A1 hex:
INTEGER*2 M M = 161 WRITE ( *, 8 ) M 8 FORMAT ( Z ) END
This example produces the following output:
demo% f77 def1.f def1.f: MAIN: demo% a.out ¤¤¤¤¤a1 demo%
The defaults for w, d, and e are summarized in the following table.
Table 5-3 Default w, d, e Values in Format Field Descriptors
Field Descriptor |
List Element |
w |
d |
e |
---|---|---|---|---|
I,O,Z |
BYTE |
7 |
- |
- |
I,O,Z |
INTEGER*2, LOGICAL*2 |
7 |
- |
- |
I,O,Z |
INTEGER*4, LOGICAL*4 |
12 |
- |
- |
O,Z |
REAL*4 |
12 |
- |
- |
O,Z |
REAL*8 |
23 |
- |
- |
O,Z |
REAL*16, COMPLEX*32 |
44 |
- |
- |
L |
LOGICAL |
2 |
- |
- |
F,E,D,G |
REAL, COMPLEX*8 |
15 |
7 |
2 |
F,E,D,G |
REAL*8, COMPLEX*16 |
25 |
16 |
2 |
F,E,D,G |
REAL*16, COMPLEX*32 |
42 |
33 |
3 |
A |
LOGICAL*1 |
1 |
- |
- |
A |
LOGICAL*2, INTEGER*2 |
2 |
- |
- |
A |
LOGICAL*4, INTEGER*4 |
4 |
- |
- |
A |
REAL*4, COMPLEX*8 |
4 |
- |
- |
A |
REAL*8, COMPLEX*16 |
8 |
- |
- |
A |
REAL*16, COMPLEX*32 |
16 |
- |
- |
A |
CHARACTER*n |
n |
- |
- |
For complex items, the value for w is for each real component. The default for the A descriptor with character data is the declared length of the corresponding I/O list element. REAL*16 and COMPLEX*32 are SPARC only.
The apostrophe edit specifier is in the form of a character constant. It causes characters to be written from the enclosed characters of the edit specifier itself, including blanks. An apostrophe edit specifier must not be used on input. The width of the field is the number of characters contained in, but not including, the delimiting apostrophes. Within the field, two consecutive apostrophes with no intervening blanks are counted as a single apostrophe. You can use quotes in a similar way.
Example: apos.f, apostrophe edit (two equivalent ways):
WRITE( *, 1 ) 1 FORMAT( 'This is an apostrophe ''.') WRITE( *, 2 ) 2 FORMAT( "This is an apostrophe '.") END
The above program writes this message twice: This is an apostrophe '.
The B, BN, and BZ edit specifiers control interpretation of imbedded and trailing blanks for numeric input.
The following blank specifiers are available:
BN--If BN precedes a specification, a nonleading blank in the input data is considered null, and is ignored.
BZ--If BZ precedes a specification, a nonleading blank in the input data is considered zero.
B--If B precedes a specification, it returns interpretation to the default mode of blank interpretation. This is consistent with S, which returns to the default sign control. @
Without any specific blank specifiers in the format, nonleading blanks in numeric input fields are normally interpreted as zeros or ignored, depending on the value of the BLANK= suboption of OPEN currently in effect for the unit. The default value for that suboption is ignore, so if you use defaults for both BN/BZ/B and BLANK=, you get ignore.
Example: Read and print the same data once with BZ and once with BN:
demo% cat bz1.f * 12341234 CHARACTER LINE*18 / ' 82 82 ' / READ ( LINE, '( I4, BZ, I4 ) ') M, N PRINT *, M, N READ ( LINE, '( I4, BN, I4 ) ') M, N PRINT *, M, N END demo% f77 -silent bz1.f demo% a.out 82 8200 82 82 demo%
Note these rules for blank control:
Blank control specifiers apply to input only.
A blank control specifier remains in effect until another blank control specifier is encountered, or format interpretation is complete.
The B, BN, and BZ specifiers affect only I, F, E, D, and G editing.
Use edit descriptor $, and space, 0, or 1 for carriage control.
The special edit descriptor $ suppresses the carriage return. @
The action does not depend on the first character of the format. It is used typically for console prompts. For instance, you can use this descriptor to make a typed response follow the output prompt on the same line. This edit descriptor is constrained by the same rules as the colon (:).
Example: The $ carriage control:
* dol1.f The $ edit descriptor with space WRITE ( *, 2 ) 2 FORMAT (' Enter the node number: ', $ ) READ ( *, * ) NODENUM END
The above code produces a displayed prompt and user input response, such as:
Enter the node number: 82
The first character of the format is printed out, in this case, a blank. For an input statement, the $ descriptor is ignored.
The following first-character slew controls and actions are provided:
Table 5-4 Carriage Control with Blank, 0, 1, and +
Character |
Vertical spacing before printing |
---|---|
Blank 0 1 + |
One line Two lines To first line of next page No advance (stdout only, not files) |
If the first character of the format is not space, 0, 1, or +, then it is treated as a space, and it is not printed.
The behavior of the slew control character + is: if the character in the first column is +, it is replaced by a control sequence that causes printing to return to the first column of the previous line, where the rest of the input line is printed.
Space, 0, 1, and + work for stdout if piped through asa.
Example: First-character formatting, standard output piped through asa:
demo% cat slew1.f WRITE( *, '("abcd")') WRITE( *, '(" efg")') The blank single spaces WRITE( *, '("0hij")') The "0" double spaces WRITE( *, '("1klm")') The "1" starts this on a new page WRITE( *, '("+", T5, "nop")') The "+" starts this at col 1 of latest line END demo% f77 -silent slew1.f demo% a.out | asa | lpr demo%
The program, slew1.f produces file, slew1.out, as printed by lpr:
bcd efg hij klmnop This starts on a new page. The + of +nop is obeyed.
The results are different on a screen; the tabbing puts in spaces:
demo% cat slew1.out bcd efg hij nop This starts on a new page. The + of +nop is obeyed. demo%
See asa(1).
The space, 0, and 1, and + work for a file opened with:
Sequential access
FORM='PRINT'
Example: First-character formatting, file output:
demo% cat slew2.f OPEN( 1,FILE='slew.out',FORM='PRINT' ) WRITE( 1, '("abcd")') WRITE( 1, '("efg")') WRITE( 1, '("0hij")') WRITE( 1, '("1klm")') WRITE( 1, '("+", T5, "nop")') CLOSE( 1, STATUS='KEEP') END demo% f77 -silent slew2.f demo% a.out
The program, slew2.f, produces the file, slew2.out, that is equal to the file, slew1.out, in the example above.
Slew control codes '0', '1', and '+' in column one are in the output file as '\n', '\f', and '\r', respectively.
The A specifier is used for character type data items. The general form is:
A [ w ]
On input, character data is stored in the corresponding list item.
On output, the corresponding list item is displayed as character data.
If w is omitted, then:
For character data type variables, it assumes the size of the variable.
For noncharacter data type variables, it assumes the maximum number of characters that fit in a variable of that data type. This is nonstandard behavior. @
Each of the following examples read into a size n variable (CHARACTER*n), for various values of n, for instance, for n = 9.
CHARACTER C*9 READ '( A7 )', C
The various values of n, in CHARACTER C*n are:
Size n |
9 |
7 |
4 |
1 |
---|---|---|---|---|
Data |
Node¤Id |
Node¤Id |
Node¤Id |
Node¤Id |
Format |
A7 |
A7 |
A7 |
A7 |
Memory |
Node¤Id¤¤ |
Node¤Id |
e¤Id |
d |
¤ indicates a blank space.
Example: Output strings of 3, 5, and 7 characters, each in a 5 character field:
PRINT 1, 'The', 'whole', 'shebang' 1 FORMAT( A5 / A5 / A5 ) END
¤¤The whole sheba
The maximum characters in noncharacter types are summarized in the following table.
Table 5-5 Maximum Characters in Noncharacter Type Hollerith (nHaaa)
Type of List Item |
Maximum Number of Characters |
---|---|
BYTE LOGICAL*1 LOGICAL*2 LOGICAL*4 LOGICAL*8 INTEGER*2 INTEGER*4 INTEGER*8 REAL REAL*4 REAL*8 REAL*16 (SPARC only) DOUBLE PRECISION COMPLEX COMPLEX*8 COMPLEX*16 COMPLEX*32 (SPARC only) DOUBLE COMPLEX |
1 1 2 4 8 2 4 8 4 4 8 16 8 8 8 16 32 16 |
In f77, you can use Hollerith constants wherever a character constant can be used in FORMAT statements, assignment statements, and DATA statements.© These constants are not recommended. FORTRAN does not have these old Hollerith (n H) notations, although the FORTRAN Standard recommends implementing the Hollerith feature to improve compatibility with old programs. But such constants cannot be used as input data elements in list-directed or NAMELIST input.
For example, these two formats are equivalent:
10 FORMAT( 8H Code = , A6 ) 20 FORMAT( ' Code = ', A6 )
In f77, commas between edit descriptors are generally optional:
10 FORMAT( 5H flex 4Hible )
For compatibility with older programs, f77 also allows READs into Hollerith edit descriptors. @
Example: Read into hollerith edit descriptor--no list in the READ statement:
demo% cat hol1.f WRITE( *, 1 ) 1 FORMAT( 6Holder ) READ( *, 1 ) WRITE( *, 1 ) END demo% f77 hol1.f hol1.f: MAIN demo% a.out older newer newer demo%
In the above code, if the format is a runtime format (variable format), then the reading into the actual format does not work, and the format remains unchanged. Hence, the following program fails:
CHARACTER F*18 / '(A8)' / READ(*,F) ! ¨ Does not work. ...
Obviously, there are better ways to read into the actual format.
The I specifier is used for decimal integer data items. The general form is:
I [w [ . m ] ]
The I w and I w.m edit specifiers indicate that the field to be edited occupies w positions. The specified input/output list item must be of type integer. On input, the specified list item becomes defined with an integer datum. On output, the specified list item must be defined as an integer datum.
On input, an I w.m edit specifier is treated identically to an I w edit specifier.
The output field for the I w edit specifier consists of:
Zero or more leading blanks followed by
Either a minus if the value is negative, or an optional plus, followed by
The magnitude of the value in the form on an unsigned integer constant without leading zeros
An integer constant always has at least one digit.
The output field for the I w.m edit specifier is the same as for the I w edit specifier, except that the unsigned integer constant consists of at least m digits, and, if necessary, has leading zeros. The value of m must not exceed the value of w. If m is zero, and the value of the item is zero, the output field consists of only blank characters, regardless of the sign control in effect.
Example: int1.f, integer input:
CHARACTER LINE*8 / '12345678' / READ( LINE, '(I2, I3, I2 )') I, J, K PRINT *, I, J, K END
12 345 67
Example: int2.f, integer output:
N = 1234 PRINT 1, N, N, N, N 1 FORMAT( I6 / I4 / I2 / I6.5 ) END
1234 1234 ** 01234
The L specifier is used for logical data items. The general form is:
L w
The L w edit specifier indicates that the field occupies w positions. The specified input/output list item must be of type LOGICAL. On input, the list item becomes defined with a logical datum. On output, the specified list item must be defined as a logical datum.
The input field consists of optional blanks, optionally followed by a decimal point, followed by a T for true, or F for false. The T or F can be followed by additional characters in the field. The logical constants, .TRUE. and .FALSE.,are acceptable as input. The output field consists of w-1 blanks followed by a T for true, or F for false.
Example: log1.f, logical output:
LOGICAL A*1 /.TRUE./, B*2 /.TRUE./, C*4 /.FALSE./ PRINT '( L1 / L2 / L4 )', A, B, C END
T ¤T ¤¤¤F
Example: log2.f, logical input:
LOGICAL*4 A 1 READ '(L8)', A PRINT *, A GO TO 1 END
The program above accepts any of the following as valid input data:
t true T TRUE .t .t. .T .T. .TRUE. TooTrue f false F FALSE .f .F .F. .FALSE. Flakey
The O and Z field descriptors for a FORMAT statement are for octal and hexadecimal integers, respectively, but they can be used with any data type.@
Ow[.m] |
---|
Zw[.m] |
where w is the number of characters in the external field. For output, m, if specified, determines the total number of digits in the external field; that is, if there are fewer than m nonzero digits, the field is zero-filled on the left to a total of m digits. m has no effect on input.
A READ, with the O or Z field descriptors in the FORMAT, reads in w characters as octal or hexadecimal, respectively, and assigns the value to the corresponding member of the I/O list.
Example: Octal input, the external data field is:
654321
The first digit in the example appears in input column 1.
The program that does the input is:
READ ( *, 2 ) M 2 FORMAT ( O6 )
The above data and program result in the octal value 654321 being loaded into the variable M. Further examples are included in the following table.
Table 5-6 Sample Octal/Hex Input Values
Format |
External Field |
Internal (Octal or Hex) Value |
---|---|---|
O4 O4 O3 |
1234¤ 16234 97¤¤¤ |
1234 1623 Error: "9" not allowed |
Z5 Z5 Z4 |
A23DE¤ A23DEF 95.AF2 |
A23DE A23DE Error: "." not allowed |
The general rules for octal and hex input are:
For octal values, the external field can contain only numerals 0 through 7.
For hexadecimal values, the external field can contain only numerals 0 through 9 and the letters A through F or a through f.
Signs, decimal points, and exponent fields are not allowed.
All-blank fields are treated as having a value of zero.
If a data item is too big for the corresponding variable, an error message is displayed.
A WRITE, with the O or Z field descriptors in the FORMAT, writes out values as octal or hexadecimal integers, respectively. It writes to a field that is w characters wide, right-justified.
M = 161 WRITE ( *, 8 ) M 8 FORMAT ( Z3 ) END
The program above displays A1 (161 decimal = A1 hex):
¤A1
The letter A appears in output column 2.
Further examples are included in the following table.
Table 5-7 Sample Octal/Hex Output Value
Format |
Internal (Decimal) Value |
External (Octal/Hex) Representation |
---|---|---|
O6 O2 O4.3 O4.4 O6 | 32767 14251 27 27 -32767 | D77777 ** D033 0033 ***** |
Z4 Z3.3 Z6.4 Z5 | 32767 2708 2708 -32767 | 7FFF A94 DD0A94 ****** |
The general rules for octal and hex output are:
Negative values are written as if unsigned; no negative sign is printed.
The external field is filled with leading spaces, as needed, up to the width w.
If the field is too narrow, it is filled with asterisks.
If m is specified, the field is left-filled with leading zeros, to a width of m.
For horizontal positioning along the print line, f77 supports the forms:
TRn, TLn, Tn, nT, T
where n is a strictly positive integer. The format specifier T can appear by itself, or be preceded or followed by a positive nonzero number.
This tab reads from the nth column or writes to the nth column.
This tab reads from the nth column to the left or writes to the nth column to the left.
This tab reads from the nth column to the right or writes to the nth column to the right.
This tab tabs to the nth tab stop for both read and write. If n is omitted, this tab uses n = 1 and tabs to the next tab stop.
This tab tabs to the next tab stop for both read and write. It is the same as the nTL with n omitted; it tabs to the next tab stop.
The rules and Restrictions for tabbing are:
Tabbing right beyond the end of an input logical record is an error.
Tabbing left beyond the beginning of an input logical record leaves the input pointer at the beginning of the record.
Nondestructive tabbing is implemented for both internal and external formatted I/O. Nondestructive tabbing means that tabbing left or right on output does not destroy previously written portions of a record.
Tabbing right on output causes unwritten portions of a record to be filled with blanks.
Tabbing left requires that the logical unit allows a seek. Therefore, it is not allowed in I/O to or from a terminal or pipe.
Likewise, nondestructive tabbing in either direction is possible only on a unit that can seek. Otherwise, tabbing right or spacing with the X edit specifier writes blanks on the output.
Tab stops are hard-coded every eight columns.
The nX edit specifier indicates that the transmission of the next character to or from a record is to occur at the position n characters forward from the current position.
On input, the nX edit specifier advances the record pointer by n positions, skipping n characters.
A position beyond the last character of the record can be specified if no characters are transmitted from such positions.
On output, the nX specifier writes n blanks.
The n defaults to 1.
Example: Input, Tn (absolute tabs):
demo% cat rtab.f CHARACTER C*2, S*2 OPEN( 1, FILE='mytab.data') DO I = 1, 2 READ( 1, 2 ) C, S 2 FORMAT( T5, A2, T1, A2 ) PRINT *, C, S END DO END demo%
demo% cat mytab.data defguvwx 12345678 demo%
demo% a.out uvde 5612 demo%
The above example first reads columns 5 and 6, then columns 1 and 2.
Example: Output Tn (absolute tabs); this program writes an output file:
demo% cat otab.f CHARACTER C*20 / "12345678901234567890" / OPEN( 1, FILE='mytab.rep') WRITE( 1, 2 ) C, ":", ":" 2 FORMAT( A20, T10, A1, T20, A1 ) END demo%
demo% cat mytab.rep 123456789:123456789: demo%
The above example writes 20 characters, then changes columns 10 and 20.
Example: Input, TRn and TL n (relative tabs)--the program reads:
demo% cat rtabi.f CHARACTER C, S, T OPEN( 1, FILE='mytab.data') DO I = 1, 2 READ( 1, 2 ) C, S, T 2 FORMAT( A1, TR5, A1, TL4, A1 ) PRINT *, C, S, T END DO END demo%
demo% cat mytab.data defguvwx 12345678 demo%
demo% a.out dwg 174 demo%
The above example reads column 1, then tabs right 5 to column 7, then tabs left 4 to column 4.
Example: Output TR n and TL n (relative tabs)--this program writes an output file:
demo% cat rtabo.f CHARACTER C*20 / "12345678901234567890" / OPEN( 1, FILE='rtabo.rep') WRITE( 1, 2 ) C, ":", ":" 2 FORMAT( A20, TL11, A1, TR9, A1 ) END demo%
The run shows nothing, but you can list the mytab.rep output file:
demo% cat rtabo.rep 123456789:123456789: demo%
The above program writes 20 characters, tabs left 11 to column 10, then tabs right 9 to column 20.
The quotes edit specifier is in the form of a character constant.@It causes characters to be written from the enclosed characters of the edit specifier itself, including blanks. A quotes edit specifier must not be used on input.
The width of the field is the number of characters contained in, but not including, the delimiting quotes. Within the field, two consecutive quotes with no intervening blanks are counted as a single quote. You can use apostrophes in a similar way.
Example: quote.f (two equivalent ways):
WRITE( *, 1 ) 1 FORMAT( 'This is a quote ".' ) WRITE( *, 2 ) 2 FORMAT( "This is a quote ""." ) END
This program writes this message twice: This is a quote ".
The format specifier is R or nR, where 2 £ n £36. If n is omitted, the default decimal radix is restored.
You can specify radixes other than 10 for formatted integer I/O conversion. The specifier is patterned after P, the scale factor for floating-point conversion. It remains in effect until another radix is specified or format interpretation is complete. The I/O item is treated as a 32-bit integer.
Example: Radix 16--the format for an unsigned, hex, integer, 10 places wide, zero-filled to 8 digits, is (su, 16r, I10.8), as in:
demo% cat radix.f integer i / 110 / write( *, 1 ) i 1 format( SU, 16r, I10.8 ) end demo% f77 -silent radix.f demo% a.out DD0000006E demo%
SU is described in "Sign Editing (SU, SP, SS, S) ".
The D, E, F, and G specifiers are for decimal real data items.
The D specifier is for the exponential form of decimal double-precision items. The general form is
D [ w [ .d ] ]
The D w and D w.d edit specifiers indicate that the field to be edited occupies w positions. d indicates that the fractional part of the number (the part to the right of the decimal point) has d digits. However, if the input datum contains a decimal point, that decimal point overrides the d value.
On input, the specified list item becomes defined with a real datum. On output, the specified list item must be defined as a real datum.
In an output statement, the D edit descriptor does the same thing as the E edit descriptor, except that a D is used in place of an E. The output field for the D w.d edit specifier has the width w. The value is right-justified in that field. The field consists of zero or more leading blanks followed by either a minus if the value is negative, or an optional plus, followed by the magnitude of the value of the list item rounded to d decimal digits.
w must allow for a minus sign, at least one digit to the left of the decimal point, the decimal point, and d digits to the right of the decimal point. Therefore, it must be the case that w d+3.
Example: Real input with D editing in the program, Dinp.f:
CHARACTER LINE*24 / '12345678 23.5678 .345678' / READ( LINE, '( D8.3, D8.3, D8.3 )') R, S, T PRINT '( D10.3, D11.4, D13.6 )', R, S, T END
0.123D+05 0.2357D+02 0.345678D+00
In the above example, the first input data item has no decimal point, so D8.3 determines the decimal point. The other input data items have decimal points, so those decimal points override the D edit descriptor as far as decimal points are concerned.
Example: Real output with D editing in the program Dout.f:
R = 1234.678 PRINT 1, R, R, R 1 FORMAT( D9.3 / D8.4 / D13.4 ) END
0.123D+04 ******** ¤¤¤0.1235D+04
In the above example, the second printed line is asterisks because the D8.4 does not allow for the sign; in the third printed line the D13.4 results in three leading blanks.
The E specifier is for the exponential form of decimal real data items. The general form is:
E [ w [ .d ] [ Ee ] ]
w indicates that the field to be edited occupies w positions.
d indicates that the fractional part of the number (the part to the right of the decimal point) has d digits. However, if the input datum contains a decimal point, that decimal point overrides the d value.
e indicates the number of digits in the exponent field. The default is 2.
The specified input/output list item must be of type real. On input, the specified list item becomes defined with a real datum. On output, the specified list item must be defined as a real datum.
The output field for the E w.d edit specifier has the width w. The value is right-justified in that field. The field consists of zero or more leading blanks followed by either a minus if the value is negative, or an optional plus, followed by a zero, a decimal point, the magnitude of the value of the list item rounded to d decimal digits, and an exponent.
For the form Ew.d:
If | exponent | .le. 99, it has the form Enn or 0nn.
If 99 .le. | exponent | .le. 999, it has the form nnn.
For the form Ew.dEe, if | exponent | .le. ( 10e ) - 1, then the exponent has the form nnn.
For the form Dw.d:
If | exponent | .le. 99, it has the form Dnn or Enn or 0nn.
If 99 £ | exponent | .le. 999, it has the form nnn.
n is any digit.
The sign in the exponent is required.
w need not allow for a minus sign, but must allow for a zero, the decimal point, and d digits to the right of the decimal point, and an exponent. Therefore, for nonnegative numbers, w .le. d+6; if e is present, then w .le. d+e+4. For negative numbers, w .le. d+7; if e is present, then w .le. d+e+5.
Example: Real input with E editing in the program, Einp.f:
CHARACTER L*40/'1234567E2 1234.67E-3 12.4567 '/ READ( L, '( E9.3, E12.3, E12.6 )') R, S, T PRINT '( E15.6, E15.6, E15.7 )', R, S, T END
¤¤¤0.123457E+06¤¤¤0.123467E+01¤¤0.1245670E+02
In the above example, the first input data item has no decimal point, so E9.3 determines the decimal point. The other input data items have decimal points, so those decimal points override the D edit descriptor as far as decimal points are concerned.
Example: Real output with E editing in the program Eout.f:
R = 1234.678 PRINT 1, R, R, R 1 FORMAT( E9.3 / E8.4 / E13.4 ) END
0.123E+04 ******** ¤¤¤0.1235E+04
In the above example, E8.4 does not allow for the sign, so we get asterisks. Also, the extra wide field of the E13.4 results in three leading blanks.
Example: Real output with Ew.dEe editing in the program EwdEe.f:
REAL X / 0.000789 / WRITE(*,'( E13.3)') X WRITE(*,'( E13.3E4)') X WRITE(*,'( E13.3E5)') X END
¤¤¤¤0.789E-03 ¤¤0.789E-0003 ¤0.789E-00003
The F specifier is for decimal real data items. The general form is
F [ w [ .d ] ]
The Fw and Fw.d edit specifiers indicate that the field to be edited occupies w positions.
d indicates that the fractional part of the number (the part to the right of the decimal point) has d digits. However, if the input datum contains a decimal point, that decimal point overrides the d value.
The specified input/output list item must be of type real. On input, the specified list item becomes defined with a real datum. On output, the specified list item must be defined as a real datum.
The output field for the F w.d edit specifier has the width w. The value is right-justified in that field. The field consists of zero or more leading blanks followed by either a minus if the value is negative, or an optional plus, followed by the magnitude of the value of the list item rounded to d decimal digits.
w must allow for a minus sign, at least one digit to the left of the decimal point, the decimal point, and d digits to the right of the decimal point. Therefore, it must be the case that w.le.d+3.
Example: Real input with F editing in the program Finp.f:
CHARACTER LINE*24 / '12345678 23.5678 .345678' / READ( LINE, '( F8.3, F8.3, F8.3 )') R, S, T PRINT '( F9.3, F9.4, F9.6 )', R, S, T END
12345.678DD23.5678D0.345678
In the above example, the first input data item has no decimal point, so F8.3 determines the decimal point. The other input data items have decimal points, so those decimal points override the F edit descriptor as far as decimal points are concerned.
Example: Real output with F editing in the program Fout.f:
R = 1234.678 PRINT 1, R, R, R 1 FORMAT( F9.3 / F8.4 / F13.4 ) END
¤1234.678 ******** ¤¤¤¤1234.6780
In the above example, F8.4 does not allow for the sign; F13.4 results in four leading blanks and one trailing zero.
The G specifier is for decimal real data items. The general form is
G [ w [ .d ] ] |
|
---|---|
or: |
|
G w.d E e |
:
The D, E, F, and G edit specifiers interpret data in the same way.
The representation for output by the G edit descriptor depends on the magnitude of the internal datum. In the following table, N is the magnitude of the internal datum.
Range |
Form |
---|---|
0.1 <= N < 1.0 1.0 <= N < 10.0 ... 10(d-2) <= N <= 10(d-1) 10(d-1) <= N < 10d |
F(w-4).d, n(¤) F(w-4).(d-1), n(¤) ... F(w-4).1, n(¤) F(w-4).0, n(¤) |
If you are entering numeric data that is controlled by a fixed-column format, then you can use commas to override any exacting column restrictions.
(I10, F20.10, I4)
Using the above format reads the following record correctly:
-345,.05e-3,12
The I/O system is just being more lenient than described in the FORTRAN Standard. In general, when doing a formatted read of noncharacter variables, commas override field lengths. More precisely, for the Iw, Fw.d, Ew.d[Ee], and Gw.d input fields, the field ends when w characters have been scanned, or a comma has been scanned, whichever occurs first. If it is a comma, the field consists of the characters up to, but not including, the comma; the next field begins with the character following the comma.
The Q edit descriptor gets the length of an input record or the remaining portion of it that is unread. @ It gets the number of characters remaining to be read from the current record.
Example: From a real and a string, get: real, string length, and string:
demo% cat qed1.f * qed1.f Q edit descriptor (real & string) CHARACTER CVECT(80)*1 OPEN ( UNIT=4, FILE='qed1.data' ) READ ( 4, 1 ) R, L, ( CVECT(I), I=1,L ) 1 FORMAT ( F4.2, Q, 80 A1 ) WRITE ( *, 2 ) R, L, '"', (CVECT(I),I=1,L), '"' 2 FORMAT ( 1X, F7.2, 1X, I2, 1X, 80A1 ) END demo% cat qed1.data 8.10qwerty demo% f77 qed1.f -o qed1 qed1.f: MAIN: demo% qed1 8.10 6 "qwerty" demo%
The above program reads a field into the variable R, then reads the number of characters remaining after that field into L, then reads L characters into CVECT. Q as the nth edit descriptor matches with L as the nth element in the READ list.
Example: Get length of input record; put the Q descriptor first:
demo% cat qed2.f CHARACTER CVECT(80)*1 OPEN ( UNIT=4, FILE='qed2.data' ) READ ( 4, 1 ) L, ( CVECT(I), I=1,L ) 1 FORMAT ( Q, 80A1 ) WRITE ( *, 2 ) L, '"', (CVECT(I),I=1,L), '"' 2 FORMAT ( 1X, I2, 1X, 80A1 ) END demo% cat qed2.data qwerty demo% f77 qed2.f -o qed2 qed2.f: MAIN: demo% qed2 6 "qwerty" demo%
The above example gets the length of the input record. With the whole input string and its length, you can then parse it yourself.
Several restrictions on the Q edit descriptor apply:
The list element Q corresponds to must be of INTEGER or LOGICAL data type.
Q does strictly a character count. It gets the number of characters remaining in the input record, and does not get the number of integers or reals or anything else.
The Q edit descriptor cannot be applied for pipe files, as Q edit requires that the file be rereadable.
This descriptor operates on files and stdin (terminal) input.
This descriptor is ignored for output.
The P edit descriptor scales real input values by a power of 10. It also gives you more control over the significant digit displayed for output values.
The general form is:
Parameter |
Description |
k |
Integer constant, with an optional sign |
k is called the scale factor, and the default value is zero.
Example: I/O statements with scale factors:
READ ( 1, '( 3P E8.2 )' ) X WRITE ( 1, '( 1P E8.2 )' ) X
P by itself is equivalent to 0P. It resets the scale factor to the default value 0P. This P by itself is nonstandard.
The scale factor is reset to zero at the start of execution of each I/O statement. The scale factor can have an effect on D, E, F, and G edit descriptors.
On input, any external datum that does not have an exponent field is divided by 10k before it is stored internally.
Input examples: Showing data, scale factors, and resulting value stored:
Data |
18.63 |
18.63 |
18.63E2 |
18.63 |
Format |
E8.2 |
3P E8.2 |
3P E8.2 |
-3P E8.2 |
Memory |
18.63 |
.01863 |
18.63E2 |
18630. |
On output, with D, and E descriptors, and with G descriptors if the E editing is required, the internal item gets its basic real constant part multiplied by 10k, and the exponent is reduced by k before it is written out.
On output with the F descriptor and with G descriptors, if the F editing is sufficient, the internal item gets its basic real constant part multiplied by 10k before it is written out.
Output Examples: Showing value stored, scale factors, and resulting output:
Memory |
290.0 |
290.0 |
290.0 |
290.0 |
Format |
2P E9.3 |
1P E9.3 |
-1P E9.3 |
F9.3 |
Display |
29.00E+01 |
2.900E+02 |
0.029E+04 |
0.290E+03 |
The SU, SP, and S edit descriptors control leading signs for output. For normal output, without any specific sign specifiers, if a value is negative, a minus sign is printed in the first position to the left of the leftmost digit; if the value is positive, printing a plus sign depends on the implementation, but f77 omits the plus sign.
The following sign specifiers are available:
SP--If SP precedes a specification, a sign is printed.
SS--If SS precedes a specification, plus-sign printing is suppressed.
S--If S precedes a specification, the system default is restored. The default is SS.
SU--If SU precedes a specification, integer values are interpreted as unsigned. This is nonstandard. @
For example, the unsigned specifier can be used with the radix specifier to format a hexadecimal dump, as follows:
2000 FORMAT( SU, 16R, 8I10.8 )
The rules and restrictions for sign control are:
Sign-control specifiers apply to output only.
A sign-control specifier remains in effect until another sign-control specifier is encountered, or format interpretation is complete.
The S, SP, and SS specifiers affect only I, F, E, D, and G editing.
The SU specifier affects only I editing.
The slash ( / ) edit specifier indicates the end of data transfer on the current record.
On input, any remaining portion of the current record is skipped, and the file is positioned at the beginning of the next record. Two successive slashes (//) skip a whole record.
On output, an end-of-record is written, and a new record is started. Two successive slashes (//) produce a record of no characters. If the file is an internal file, that record is filled with blanks.
Each slash increases the record number by one, and the file is positioned at the start of the record with that record number.
On output, two successive slashes (//) produce a record of no characters, and that record is filled with blanks.
The colon (:) edit descriptor allows for conditional termination of the format. If the I/O list is exhausted before the format, then the format terminates at the colon.
* col1.f The colon (:) edit descriptor DATA INIT / 3 /, LAST / 8 / WRITE ( *, 2 ) INIT WRITE ( *, 2 ) INIT, LAST 2 FORMAT ( 1X 'INIT = ', I2, :, 3X, 'LAST = ', I2 ) END
The above program produces output like the following
INIT = 3 INIT = 3 LAST = 8
Without the colon, the output is more like this:
INIT = 3 LAST = INIT = 3 LAST = 8