Formatted Statements [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation
HP FORTRAN 77/iX Programmer's Guide
Formatted Statements
Formatted I/O statements use format descriptors that supplement the READ,
WRITE, or PRINT statements to exactly define the format of the data. The
descriptors can be specified on the READ, WRITE, or PRINT statements, or
can appear in a FORMAT statement.
The FORMAT statement is a nonexecutable statement that describes how the
data listed in a READ, WRITE, or PRINT statement is to be arranged. The
FORMAT statement can appear anywhere in a program after a PROGRAM,
FUNCTION, or SUBROUTINE statement.
The type of conversion indicated by the format descriptors should
correspond to the data type of the variable in the I/O list. The format
descriptors are described later in this chapter.
Formatted Input
The formatted READ statement transfers data from an external device to
internal storage, and converts the ASCII data to internal representation
according to the format descriptor.
One way of specifying a formatted READ statement is to place the
descriptors on the READ statement itself. For example, the statement:
READ (5, '(I5, F5.1)') int, value
reads data from unit 5 into int and value according to the format
descriptors I5 and F5.1, respectively.
If there are many format descriptors or if the same descriptors are used
repeatedly, place the descriptors in a FORMAT statement. To specify a
FORMAT statement, use one of the following forms of a formatted READ
statement. The statements:
READ (5,100) a, b, c
100 FORMAT (F7.1, F8.1, F9.1)
read data from unit 5 into the variables a, b, and c according to the
format descriptors F7.1, F8.1, and F9.1, respectively.
The statements:
READ 100, a, b, c
100 FORMAT (F7.1, F8.1, F9.1)
get data from the standard input file according to the format descriptors
in the FORMAT statement labeled 100.
The following program shows several ways of writing formatted input
statements.
PROGRAM formatted_input
C Formatted input statements from the preconnected input unit:
READ(5, '(I9)') i
READ(5, 100) i
100 FORMAT(I9)
C Formatted input statements from the standard input device:
READ(*, '(I9)') i
READ(*, 100) i ! Note: These statements reuse the
READ 100, i ! FORMAT statement labeled 100 above.
READ '(I9)', i
END
Formatted Output
The formatted WRITE and PRINT statements transfer data from the storage
location of the variables or expressions named in the output list to the
file associated with the specified unit. The data is converted to a
string of ASCII characters according to the format descriptors.
One way of specifying a formatted WRITE statement is to include the
format descriptors on the WRITE statement itself. For example, the
statement:
WRITE (6, '(1X, I5, F3.1)') int, value
writes the values of int and value to unit 6 (the preconnected output
unit) according to the format descriptors I5 and F3.1, respectively. The
statement:
PRINT '(1X, I5, F3.1)', int, value
also writes data from int and value to the standard output device
according to the format descriptors I5 and F3.1.
To use a FORMAT statement, specify its label in the corresponding WRITE
or PRINT statements. For example, the statements:
WRITE (6,100) int, value
100 FORMAT (1X, I5, F3.1)
write data from variables int and value to the preconnected output unit
according to the descriptors in the FORMAT statement labeled 100.
The following program shows various ways of writing formatted output
statements.
PROGRAM formatted_output
i = 15
C Formatted output statements to preconnected output unit:
WRITE(6, '(1X, "i=", I9)') i ! The output of these
WRITE(6,200) i ! statements looks the
200 FORMAT (1X, 'i=', I9) ! same.
C Formatted output statements to standard output device:
WRITE(*, '(1X, "i=", I9)') i
WRITE(*, 200) i
C Note: This statement reuses the FORMAT statement labeled 200 above
PRINT '(1X, "i=", I9)', i
PRINT 200, i
C Note: This statement reuses the FORMAT statement labeled 200 above
END
Variable Format Descriptors
Variable format descriptors allow the values of integer variables,
integer constants, and character constants to be imported into format
strings. Integer variable format descriptors may be used wherever an
integer may appear, except to specify the number of characters in a
Hollerith field. To use a variable format descriptor, enclose the
variable or constant in angle brackets. The following is an example of a
variable format descriptor:
FORMAT (I<isize>)
In this example, the FORMAT statement performs an I (integer) data
transfer with a field width equal to the value of isize when the format
is scanned.
Variables may be INTEGER*2 or INTEGER*4. The value of a variable format
descriptor must be of a valid magnitude for its use in the format;
otherwise an error occurs.
Variable format descriptors are not allowed in run-time formats (that is,
those that are assembled in arrays or character expressions at run-time).
If a variable is used, its value is reevaluated each time it is
encountered in the normal format scan. If the value of a variable used
in a descriptor changes during execution of the I/O statement, the new
value is used the next time the format item containing the descriptor is
processed.
The following example program illustrates the use of variable format
descriptors.
C Program illustrating variable format descriptors.
PROGRAM varfmt1
INTEGER n
PARAMETER (n = 3)
REAL x(n,n)
DATA x / 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 /
C Print out the constants 1 through 3 in variable width fields.
DO 10 j = 1,3
PRINT 100,j
100 FORMAT (1x, I<j>)
10 CONTINUE
C Print out the lower diagonal elements of matrix x.
DO 20 i = 1,n
PRINT 101, (x(i,k), k = 1,I)
101 FORMAT (1x, <I>F5.1)
20 CONTINUE
END
The output of of the program is as follows:
1
2
3
1.0
2.0 5.0
3.0 6.0 9.0
MPE/iX 5.0 Documentation