Format Specifications (continued) [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Programmer's Guide
Format Specifications (continued)
Handling Character Positions
The X, T, TL, and TR edit descriptors handle character position control.
The X Descriptor.
The X edit descriptor skips character positions in an input or output
line. The form of the descriptor is:
nX
where n is the number of positions to be skipped from the current
position; n must be a positive nonzero integer.
On input, the X edit descriptor causes the next n positions of the input
line to be skipped. On output, the X descriptor causes n positions of
the output line to be filled with blanks, if not previously defined;
these positions are not otherwise written. The X descriptor is identical
to the TR descriptor.
For example, the following program uses the X descriptor for input and
output.
PROGRAM x_edit
INTEGER a, b
READ (5, 100) a, b, c
100 FORMAT (2X, I3, 5X, I3, F9.4)
WRITE (6,200) a, b, c
200 FORMAT (5X, I3, 5X, I3, 5X, F9.4)
END
If the input to this program is:
Delta Delta 123 Delta Delta Delta Delta Delta 1231234.5678
the program produces this output:
Delta Delta Delta Delta Delta 123 Delta Delta Delta Delta Delta 123 Delta Delta Delta Delta Delta 1234.5678
The T Descriptor.
The T edit descriptor provides tab control. The form of the descriptor
is:
Tn
where n is a positive, nonzero integer indicating number of columns.
When the T edit descriptor is on a format line, input or output control
skips right or left to the character position n; the next descriptor is
then processed. Be careful not to skip beyond the length of the record.
For example, consider this program:
PROGRAM t_edit
READ (5, 100) a, b
100 FORMAT (T6, F4.1, TI5, F6.2)
PRINT 200, a, b
200 FORMAT (F4.1, 5X, F6.2)
END
If the input to this program is:
Delta Delta Delta Delta Delta 12.3 Delta Delta Delta Delta Delta 123.45
uparrow uparrow
Column Column
6 15
the value stored in a is 12.3 and the value stored in b is 123.45.
Using the T edit descriptor, you can write over fields; that is, you can
destroy a previously formed field. For example, the program:
PROGRAM t_edit_2
OPEN (3, FILE='writeit')
WRITE (3, 100)
100 FORMAT (1X, '1234567890', T4, 'abcde')
CLOSE (3)
END
writes the following string to file writeit:
12abcde890
Similarly, you can also reread fields with the T descriptor.
The TL Descriptor.
The TL edit descriptor provides tab control. The form of the descriptor
is:
TLn
where n is a positive, nonzero integer indicating number of columns.
When the TL edit descriptor is on a format line, input or output control
skips left n column positions from the current cursor position. If n is
greater or equal to the current cursor position, the control goes to the
first column position.
For example, consider this program:
PROGRAM tl_edit
OPEN (3, FILE='datafile')
READ (3, 100) a, b
100 FORMAT (F6.2, TL6, F6.2)
PRINT 200, a, b
200 FORMAT (1X, F6.2, 5X, F6.2)
CLOSE (3)
END
If the file datafile contains the data:
123.45
the output looks like this:
123.45 123.45
Using the TL edit descriptor, you can write over fields. For example,
the program
PROGRAM tl_edit
OPEN (3, FILE='writeit')
WRITE(3, 100)
100 FORMAT(1X, 'It is winter ', TL7, 'summer.')
CLOSE(3)
END
writes the line:
It is summer.
to the file writeit.
The TR Descriptor.
The TR edit descriptor provides tab control. The form of the descriptor
is:
TRn
where n is a positive, nonzero integer indicating number of columns.
When the TR edit descriptor is on a format line, input or output control
skips right n column positions from the current cursor position. Be
careful not to skip beyond the length of the record.
For example, the program:
PROGRAM tr_edit
a = 123.4
b = 1234.11
WRITE (6, 100) a, b
100 FORMAT (1X, F5.1, TR5, F7.2)
END
produces the following output:
123.4 Delta Delta Delta Delta Delta 1234.11
Handling Literal Data
The ', ", and H edit descriptors handle literal data.
The ' and " Descriptors.
Paired apostrophe (') and quotation mark (") edit descriptors write
character strings; the paired symbols delimit a string of characters,
which can include blanks. The ' and " descriptors are preferred over the
H edit descriptor.
If the character string contains an apostrophe or a quotation mark, you
can do one of the following:
* Delimit the symbol with two marks of the same type
* Use the other symbol as the delimiter
For example, the program:
PROGRAM literal_edit
WRITE (6,100) ! String uses apostrophes
100 FORMAT (1X, 'Enter your name:')
WRITE (6,200) ! String uses quotation marks
200 FORMAT (1X, "Enter your address:")
WRITE (6,300) ! Statement is continued on two lines
300 FORMAT (1X, 'Enter your employee number',
* ' and employee location code:')
WRITE (6,400) ! Quotation mark with two marks of the same type
400 FORMAT (1X, 'What''s your home telephone number?')
WRITE (6,500) ! Quotation mark with other symbol as delimiter
500 FORMAT (1X, "What's your work telephone number?")
END
produces the following output:
Enter your name:
Enter your address:
Enter your employee number and employee location code:
What's your home telephone number?
What's your work telephone number?
The H Descriptor.
The H (Hollerith) edit descriptor writes character strings. The H
descriptor has the form:
nHstring
where n is the number of characters in the string and string is the
string of characters. The string of characters is not delimited with
quotation marks.
For example, the program:
PROGRAM h_edit
pi = 3.14159
WRITE (6, 100) pi
100 FORMAT (1X, 21HThe value of "pi" is , F7.5)
END
produces the following output:
The value of "pi" is 3.14159
The H descriptor is provided for compatability with older versions of
FORTRAN; its use is discouraged.
Using Scale Factors: The P Descriptor
The P edit descriptor scales real numbers on input or output. The
descriptor has the form:
nP
where n is the integer scale factor. The P descriptor can precede the D,
E, and G format descriptors for input and output without an intervening
comma or other separator.
Once a P descriptor is specified, the scale factor holds for all
subsequent descriptors on the FORMAT statement until another scale factor
is defined. A scale factor of zero (0P) ends the effect of the scale
factor.
On input, the scale factor affects fixed-field values; the value is
multiplied by 10 raised to the -nth power. However, if the input number
includes an exponent, the scale factor has no effect.
For example, this program shows how the P descriptor affects numeric
values:
PROGRAM p_edit_input
READ (5, '(G8.4)') value1 ! Multiply by 10**0
READ (5, '(-2PG8.4)') value2 ! Multiply by 10**(2)
READ (5, '(2PG8.4)') value3 ! Multiply by 10**(-2)
READ (5, '(2PG8.4)') value4 ! If input includes an exponent,
* the scale factor has no effect.
END
If the input to this program is:
123.4567
123.4567
123.4567
123.45E0
the values stored are as follows:
value 1 = 123.4567
value 2 = 12345.67
value 3 = 1.234567
value 4 = 123.4500
On output, the scale factor affects the D, E, and F format descriptors.
The scale factor affects the G format descriptor only if Gw.d is
interpreted as Ew.d.
When using the P edit descriptor with the D, E, or G format descriptor,
the forms are as follows:
nPD or nPDw.d
nPE or nPEw.d or nPEw.dEe
nPG or nPGw.d or nPGw.dEe
n is a required value. If you do not specify n, the default value will
be 1. w.d and Ee are both optional, but if you specify Ee, you must
specify w.d
When using the P edit descriptor with the G format descriptor, the input
or output is dependent on the value to be read or written. The scale
factor nP shifts the decimal point to the right n places and reduces the
exponent by n.
When using the P edit descriptor with the F format descriptor, the form
is as follows:
nPFw.d
The internal value is multiplied by 10n.
For example, the program:
PROGRAM p_edit_output
pi = 3.14159
C Write without a scale factor
WRITE (6, '(2X, "FORMAT", 10X, "VALUE"/)')
WRITE (6, '(1X, " D10.4", 5X, D10.4/)') pi
C Write with a scale factor on the D and E format descriptors
WRITE (6, '(1X, "-3PD10.4", 5X, -3PD10.4)') pi
WRITE (6, '(1X, "-1PE10.4", 5X, -1PE10.4)') pi
WRITE (6, '(1X, " 1PE10.4", 5X, 1PE10.4)') pi
WRITE (6, '(1X, " 3PD10.4", 5X, 3PD10.4)') pi
WRITE (6, '(1X, " 3PE10.4", 5X, 3PE10.4/)') pi
C Write with a scale factor on the F format descriptor
WRITE (6, '(1X, "-1PF10.4", 5X, -1PF10.4)') pi
WRITE (6, '(1X, " PF10.4", 5X, PF10.4)') pi
WRITE (6, '(1X, " 5PF10.4", 5X, 5PF10.4)') pi
END
produces the following output:
FORMAT VALUE
D10.4 .3142D+01
-3PD10.4 .0003D+04
-1PE10.4 .0314E+02
1PE10.4 3.1416E+00
3PD10.4 314.16E-02
3PE10.4 314.16E-02
-1PF10.4 .3142
PF10.4 31.4159
5PF10.4 **********
NOTE The last field is filled with asterisks because the field width w
is smaller than the number of digits needed to represent the value.
If the P descriptor does not precede a D, E, F, or G descriptor, it
should be separated from other descriptors by commas or slashes. For
example, the statement:
100 FORMAT(1X, 2P, 3(I5, F7.2))
scales the F format descriptor value.
If the P descriptor does precede a D, E, F, or G descriptor, the comma or
slash is optional.
For example, the program:
PROGRAM p_edit_output_2
int = 5
real = 2.2
pi = 3.14159
* Output values without a scale factor:
WRITE (6,50) int, real, pi
50 FORMAT (1X, I2, 3X, F14.4, 3X, E15.4/)
* Output values with a scale factor:
WRITE (6,100) int, real, pi
100 FORMAT (1X, I2, 3X, 3P, F14.4, 3X, E15.4)
* Show that FORMAT statements 100, 200, and 300 are equivalent:
WRITE (6,200) int, real, pi
200 FORMAT (1X, 3P, I2, 3X, F14.4, 3X, E15.4)
WRITE (6,300) int, real, pi
300 FORMAT (1X, I2, 3X, 3PF14.4, 3X, E15.4)
END
produces the following output:
5 2.2000 .3142E+01
5 2200.0000 314.16E-02
5 2200.0000 314.16E-02
5 2200.0000 314.16E-02
Note that the P descriptor has no effect on the I2 format descriptor.
Printing Plus Signs: The S, SP, and SS Descriptors
The S, SP and SS edit descriptors can be used with the D, E, F, G, and I
format descriptors to control the printing of optional plus signs (+) in
numeric output. A formatted output statement does not usually print the
plus signs. However, if an SP edit descriptor is in a format
specification, all succeeding positive numeric fields will have a plus
sign. The field width w must be large enough to contain the sign. When
an S or SS edit descriptor is encountered, the optional plus signs are
not printed.
For example, the program:
PROGRAM s_edit
int = 12345
WRITE(6,100) int ! By default, + is not printed
100 FORMAT(1X, I5)
WRITE(6,200) int, int ! With SP, the + is printed
200 FORMAT(1X, SP, I6, /, 1X, S, I6) ! With S, the + is not printed
END
produces the following output:
12345
+12345
12345
Returning the Number of Bytes: The Q Descriptor
The Q edit descriptor returns the number of bytes remaining on the
current input record. The value is returned to the next item on the
input list, which must be an integer variable. This descriptor applies
to input only and is ignored by the WRITE or PRINT statements.
For example, in the program:
PROGRAM q_format_input
CHARACTER string(80)
READ(5,100) len, (string(i), i = 1, min(len, 80))
100 FORMAT(Q, 80A1)
END
the variable len gets assigned the current length of the string. The Q
edit descriptor is used to avoid an error from reading more bytes from
the input record than are available, or from having blanks provided that
were not in the input file.
Terminating Format Control: The Colon Descriptor
The colon (:) edit descriptor conditionally terminates format control,
just as if the final right parenthesis in the FORMAT statement has been
reached. If there are more items in the I/O list, the colon edit
descriptor has no effect.
For example, the program:
PROGRAM colon_edit
value1 = 12.12
value2 = 34.34
value3 = 56.56
WRITE(6,100) value1, value2, value3
100 FORMAT(1X, 'Values = ', 3(F5.2, :, ', '))
END
produces the following output:
Values = 12.12, 34.34, 56.56
The format control terminated after the value of value3 was printed, not
after the final comma was printed.
The colon has no effect on input except if a / descriptor is included.
For example, if the contents of file datafile are:
12
24
36
48
the program:
PROGRAM example1
OPEN(9, FILE='datafile')
READ (9,10) i
READ (9,10) j
10 FORMAT (5(I2, /))
PRINT *, i
PRINT *, j
CLOSE(9)
END
produces the following output:
12
36
However, the program:
PROGRAM example2
OPEN(9, FILE='datafile')
READ (9,10) i
READ (9,10) j
10 FORMAT (5(I2, :, /))
PRINT *, i
PRINT *, j
CLOSE(9)
END
produces this output:
12
24
In the first example, the / descriptor causes the record containing the
value 24 to be skipped. In the second example, the colon terminates
format control before the / descriptor because no more list items remain
in the I/O list.
Handling Blanks in the Input Field
The BN and BZ edit descriptors are used to handle blanks in the input
field.
The BN Descriptor.
The BN edit descriptor is used with the D, E, F, G, I, O, K, @, and Z
format descriptors to interpret blanks in numeric input fields. If BN is
specified, all embedded blanks are ignored, the input number is
right-justified within the field width, and, if needed, the field is
padded with leading blanks. An input field of all blanks has a value of
zero. If the BN or BZ descriptors are not specified, the treatment of
blanks is as if you specified the BN edit descriptor. An exception to
this default is when the unit is connected with BLANK='ZERO' specified in
the OPEN statement, as described in Chapter 3 .
For example, consider this program:
PROGRAM bn_edit
READ (5, 100) int1, val1, int2
100 FORMAT (I3, BN, F6.2, I3)
END
If the input to this program is:
1 Delta Delta Delta 4 Delta . Delta 2 Delta 1 Delta
the variables int, val1, and int2 have the following values:
int1 = 1
val1 = 4.2
int2 = 1
The BN edit descriptor remains in effect until a BZ edit descriptor
(described below) is encountered or until the end of the format
specification.
For example, the following program uses the BZ descriptor to cancel the
BN descriptor's treatment of blanks.
PROGRAM bn_bz_edit
READ (5, 100) int1, int2, int3
100 FORMAT (I5, BN, I3, BZ, I5)
PRINT *, int1, int2, int3
END
If the input line is:
1 Delta Delta Delta Delta 2 Delta Delta 3 Delta Delta Delta Delta
the variables int1, int2, and int3 have the following values:
int1 = 1
int2 = 2
int3 = 30000
The BZ Descriptor.
The BZ edit descriptor is used with the D, E, F, G, I, O, K, @, and Z
format descriptors to interpret blanks in numeric input fields. If BZ is
specified, trailing and embedded blanks are interpreted as zeros. An
input field of all blanks has a value of zero.
For example, consider this program:
PROGRAM bz_edit
READ (5,100) int1, val1, int2
100 FORMAT (I3, BZ, F6.2, I3)
END
If the input to this program is:
1 Delta Delta Delta 4 Delta . Delta 2 Delta 1 Delta
the variables int, val1, and int2 have the following values:
int1 = 1
val1 = 40.02
int2 = 10
The BZ edit descriptor remains in effect until a BN edit descriptor is
encountered or until the end of the format specification.
For example, the program below uses the BN descriptor to end the
interpretation of blanks as zeros.
PROGRAM bn_bz_edit
READ (5,100) int1, int2, int3
100 FORMAT (I5, BZ, I3, BN, I5)
PRINT *, int1, int2, int3
END
If the input line is:
1 Delta Delta Delta Delta 2 Delta Delta 3 Delta Delta Delta Delta
the variables int1, int2, and int3 have the following values:
int1 = 1
int2 = 200
int3 = 3
MPE/iX 5.0 Documentation