Programmer Guide > Format Strings > FORTRAN Format Code Descriptions
  

FORTRAN Format Code Descriptions
This section describes the following format codes:
* A Format Code
* : Format Code
* $ Format Code
* F, D, E, and G Format Codes
* I, O, And Z Format Codes
* Q Format Code
* H Format Codes and Quoted Strings
* T Format Code
* TL Format Code
* TR and X Format Codes
A Format Code
The A format code transfers character data.
Format
[n]A[w]
where:
*n—Optional repeat count (1 n 32767) specifying the number of times the format code should be processed. If n is not specified, a repeat count of 1 is used.
*w—Optional width (1 w 256), specifying the number of characters to be transferred. If w is not specified, the entire string is transferred. If w is greater than the length of the string, only the number of characters in the string is transferred. Since PV-WAVE strings have dynamic length, w specifies the resulting length of input string variables.
 
note
During input, if the Q FORTRAN format specifier is used, the number of characters in the input record can be queried and used as a “parameter” in a subsequent A FORTRAN format specifier.
Example
For example, the statement:
PRINT, Format='(A6)', '123456789' 
generates the output:
123456
: Format Code
The colon format code terminates format processing if there are no more data remaining in the argument list.
Example
For example, the following statement:
PRINT, Format='(6(I1, :, ", "))', INDGEN(6) 
outputs a comma separated list of integer values:
0, 1, 2, 3, 4, 5
The use of the colon format code prevents a comma from being output following the final item in the argument list.
$ Format Code
When PV-WAVE completes output format processing, it normally issues a newline to terminate the output operation. However, if a $ format code is found in the format specification, this default newline is not output.
 
note
The $ format code is only used during output; it is ignored during input formatting.
Example
The most common use for the $ format code is in prompting for user input. For example, the following statements:
; Prompt for input, suppressing any <Return>.
PRINT, Format='($, "Enter Value: ")'
; Read the response.
READ, value
prompts for input without forcing the user’s response to appear on a separate line from the prompt.
F, D, E, and G Format Codes
The F, D, E, and G, format codes are used to transfer floating-point values between memory and the specified file.
Format
[n]F[w.d]
[n]D[w.d]
[n]E[w.d] or [n]E[w.dEe]
[n]G[w.d] or [n]G[w.dEe]
where:
*n—Optional repeat count (1 n 32767) specifying number of times to process the format code. If n is not specified, a repeat count of 1 is used.
*w.d—Optional width specification (0 w 256, 1 d < w). w specifies the number of characters in the external field, and d specifies the number of decimal positions.
*e—Optional width (1 e 256) specifying the width of exponent part of the field. PV-WAVE ignores this value, but it is allowed to maintain compatibility with FORTRAN.
During input, the F, D, E, and G format codes all transfer w characters from the external field and assign them as a real value to the corresponding entry in the I/O argument list.
The F and D format codes output values using fixed-point notation. The value is rounded to d decimal positions and right-justified into an external field that is w characters wide. The value of w must be large enough to include a minus sign when necessary, at least one digit to the left of the decimal point, the decimal point, and d digits to the right of the decimal point. The code D is identical to F (except for its default values for w and d) and exists in PV-WAVE primarily to maintain compatibility with FORTRAN. The defaults for w, d, and e are shown in the following table:
 
Table A-3: Floating-point Format Defaults
Data Type
w
d
e
Float, Complex
15
7
2
Double
25
16
2
All Other Types
25
16
2
The E format code is used for scientific (exponential) notation. The value is rounded to d decimal positions and right justified into an external field that is w characters wide. The value of w must be large enough to include a minus sign when necessary, at least one digit to the left of the decimal point, the decimal point, d digits to the right of the decimal point, a plus or minus sign for the exponent, the character “e” or “E”, and at least two characters for the exponent.
The G format code is a compromise between these choices—it uses the F output style when reasonable and E for other values.
 
note
During output, if the field provided is not wide enough, it is filled with asterisks (*) to indicate the overflow condition. If w is zero, the “natural” width for the value is used—the value is output using a default format without any leading or trailing white space, in the style of the C standard I/O library printf(3S) function.
If w, d, or e are omitted, the values listed in the previous table are used.
The case of the format code is ignored by PV-WAVE except during output. For output, the case of the E and G format codes determines the case used to output the exponent in scientific notation. Examples of Floating Point Output gives examples of several floating-point formats and the resulting output.
 
Table A-4: Examples of Floating Point Output
Format
Internal Value
Formatted Output
F
100.0
___100.0000000
F
100.0D
____100.0000000000000000
F10.0
100.0
____100.
F10.1
100.0
____100.0
F10.4
100.0
__100.0000
F2.1
100.0
**
e10.4
100.0
1.0000e+02
E10.4
100.0
1.0000E+02
g10.4
100.0
____100.0
g10.4
10000000.0
_1.000e+07
I, O, And Z Format Codes
The I, O, and Z, format codes are used to transfer integer values between memory and the specified file. The I format code is used for decimal values, O is used for octal values, and Z is used for hexadecimal values.
Format
[n]I[w] or [n]I[w.m]
[n]O[w] or [n]O[w.m]
[n]Z[w] or [n]Z[w.m]
where:
*n—is an optional repeat count (1 n 32767) specifying the number of times the format code should be processed. If n is not specified, a repeat count of 1 is used.
*w—is an optional integer value (0 w 256) specifying the width of the field in characters. The default values used if w is omitted are listed in Integer Format Defaults. If the field provided is not wide enough, it is filled with asterisks (*) to indicate the overflow condition.
 
note
If w is zero, the “natural” width for the value is used—the value is output using a default format without any leading or trailing white space, in the style of the C standard I/O library printf(3S) function.
 
Table A-5: Integer Format Defaults
Data Type
w
Byte, Integer
7
Long (32-bit), INT32
12
Long (64-bit)
21
*m—is the minimum number of non-blank digits required (1 m 256); this occurs only during output. The field is zero-filled on the left if necessary. If m is omitted or zero, the external field is blank filled.
The case of the format code is ignored by PV-WAVE, except during output. For output, the case of the Z format codes determines the case used to output the hexadecimal digits A-F. Examples of Integer Output gives examples of several integer formats and the resulting output.
 
Table A-6: Examples of Integer Output  
Format
Internal Value
Formatted Output
I
3000
__3000
I6.5
3000
_03000
I5.6
3000
*****
I2
3000
**
O
3000
__5670
I6.5
3000
_05670
O5.6
3000
*****
O2
3000
**
z
3000
____bb8
Z
3000
____BB8
Z6.5
3000
_00bb8
Z5.6
3000
*****
Z2
3000
**
Q Format Code
The Q format code returns the number of characters in the input record remaining to be transferred during the current read operation. It is ignored during output formatting.
Format
Q
Q is useful for determining how many characters have been read on a line. It can also be used to query the number of characters in the input record for later use as a “parameter” in the following A FORTRAN format specifier.
Example
The following statements count the number of characters in a file demo.dat:
; Open the file for reading.
OPENR, 1, "demo.dat"
; Create a longword integer to keep the count.
n = 0L
; Count the characters.
WHILE(not EOF(1)) DO BEGIN READF, 1, $
cur, Format='(Q)' & n = n + cur &
END
; Report the result.
PRINT, n, Format='("Counted", I, "characters.")'
; Done with the file.
CLOSE, 1 
H Format Codes and Quoted Strings
Format
The format for a Hollerith constant is:
nHc1c2c3...cn
where:
n—is the number of characters in the constant (1 n 255).
ci—represents the characters that make up the constant. The number of characters must agree with the value provided for n.
During output, any quoted strings or Hollerith constants are sent directly to the output. During input, they are ignored.
Example
For example, the statement
PRINT, Format='("Value: ", I0)', 23
results in
Value:	 23
being output. Notice the use of single quotes around the entire format string and double quotes around the quoted string inside the format. This is necessary because we are including quotes inside a quoted string. It would have been equally correct to use double quotes around the entire format string and single quotes internally. Another way to specify the string is with a Hollerith constant:
PRINT, Format='(7HValue: , I0)', 23
 
note
The zero width of the integer format string (I) results in the “natural” width being used to output the value ‘23’.
T Format Code
The T format code specifies the absolute position in the current external record.
Format
Tn
where:
n—is the absolute character position within the external record to which the current position should be set (1 n 255).
 
note
T differs from the TL, TR, and X format codes primarily in that it requires an absolute position rather than an offset from the current position.
Example
For example:
PRINT, Format= '("First", 20X, "Last", T10, "Middle")' 
produces the following output:
First     Middle         Last
TL Format Code
The TL format code moves the current position in the external record to the left.
Format
TLn
where:
n—is the number of characters to move left from the current position (1 n 255). If the value of n is greater than the current position, the current position is moved to Column 1.
 
note
TL is used to move backwards in the current record. It can be used during input to read the same data twice, or during output to position the output nonsequentially.
Example
For example:
PRINT, Format='("First", 20X, "Last", TL15, "Middle")'
produces the following output:
First         Middle     Last
TR and X Format Codes
The TR and X format codes move the current position in the external record to the right.
Format
TRn
nX
where:
n—is the number of characters to skip (1 n 255). During input, n characters in the current input record will be skipped. During output, the current output position is moved n characters to the right.
The TR or X format code can be used to leave blanks in the output record, or to skip over unwanted values while reading data.
Example
For example:
PRINT, Format='("First", 15X, "Last")' 
or
PRINT, Format='("First", TR15, "Last")' 
results in the output:
First               Last
These two format codes only differ in one way: using the X format code at the end of an output record will not cause any characters to be written unless it is followed by another format code that causes characters to be output. The TR format code always writes characters in this situation. Thus:
PRINT, Format='("First", 15X)' 
does not leave 15 blanks at the end of the line, but the following statement does:
PRINT, Format='("First", 15TR)' 

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.