CHAPTER 6
FORMATTED INPUT/OUTPUT
The emphasis of Chapter 2 was on unformatted input/output. Generally, most programmers use unformatted input and formatted output, with some exceptions.
Unformatted input is easier to work with; formatted output has
the advantage that it can be made to look exactly as
desired. This chapter deals with formatted input/output.
6.1 FORMAT STATEMENT
A FORMAT statement is a nonexecutable statement
linked to a READ or WRITE statement, which can be
placed anywhere in a program unit after the PROGRAM
statement and before the END statement (refer to Fig. 1.1).
It consists of a statement label and the keyword FORMAT
followed by a format specification enclosed in parentheses.
Both statement label and format specification are
required. Within one program unit, two format statements
cannot have the same label.
A FORMAT statement has the following form:
label FORMAT(f1, f2, f3, f4, f5, f6, ..., fn)
where label represents the statement label, and f1, f2, ... is
the list of field and edit descriptors. These constitute the
format specification and must be separated by commas and
enclosed in parentheses. For example:
105 FORMAT(I10,F10.3,E12.5)
A record is a line of input or output, separated from
other records by a carriage return. The left parenthesis is
the record initiator and the right parenthesis is the record
terminator. Both are required. The pair of parentheses
causes the transfer of one record according to the format
specification enclosed within them.
The slash (/) in a format specification is a record
separator, which is both a record terminator and initiator. The
presence of a slash causes the current record to terminate
and the next record to initiate, thereby increasing the
number of records transferred by one.
The record is transferred according to the format
specified within the respective initiator-terminator pair. For
example,
105 FORMAT(I10,F10.3/E12.5)
results in the transfer of two records.
115 FORMAT(/I10,F10.3/E12.5,E13.6/)
results in the transfer of four records, the first and last
being empty (null) records. The null record skips one line
of input and produces a blank line of output. Hence,
125 FORMAT()
skips one line, and
135 FORMAT(///)
skips four lines.
Alternatively, the repeat slash
135 FORMAT(3(/))
will also skip four lines.
The following are examples of FORMAT statements,
described in detail later in this chapter.
150 FORMAT(A20,I10,F10.3,E12.5/)
230 FORMAT(//8X,2I5,3F12.5,5(/))
The letters A, I, F, and E are format codes of field descriptors, and X is a format code of an edit descriptor. The
comma is required as a field or edit descriptor separator.
The comma is also required preceding a repeat slash, as in
the last example. The comma is optional when preceding
or following one or more slashes, as shown in the following versions of the previous examples:
150 FORMAT(A20,I10,F10.3,E12.5,/)
230 FORMAT(//,8X,2I5,3F12.5,5(/))
6.2 FIELD AND EDIT DESCRIPTORS
A field descriptor specifies the size and form of a data
item. An edit descriptor affects the location of the data
item in the external field. Field and edit descriptors are
specified with format codes, which are listed in Table 6.1.
Regarding input, a data item is transferred from an external field (on the screen or in an input file) to an internal
value (in the computer's memory). As the programmer,
you work with and see only the external field.
Regarding output, a data item is transferred from an internal value to an external field (to the screen or to an output file).
TABLE 6.1 FORMAT CODES
Code |
Type |
Function |
Form |
I
F
E
D
L
A
H
X
T
$ |
Field
Field
Field
Field
Field
Field
Field
Edit
Edit
Edit |
Transfers integer values
Transfers real values
Transfers real values with E exponent
Transfers real values with D exponent
Transfers logical values
Transfers character values
Transfers character constants
Specifies character skipping
Specifies positional tabulation
Specifies suppression of carriage return |
I w
Fw.d
Ew.d
Dw.d
Lw
Aw
nH...
nX
Tn
$ |
Integer Field Descriptor I
This field descriptor takes the form
Iw
where I is the format code for integer values and w is the
field width, which is the number of spaces allotted. For
example:
I5
specifies an integer format occupying up to 5 spaces.
In output, the integer field descriptor has also the form Iw. Optionally, it may take the form
Iw.n
where n is the minimum number of characters that must
appear within the field. For example:
I5.3
In this case, assume that 27 is the number to be printed.
The output is allowed 5 spaces, and a minimum of three
digits must be shown. Since 27 has two digits, an additional leading zero will be printed. Therefore, the output
will appear as bb027. (Throughout this chapter, a lower
case b represents a blank space).
The value of n should be less than w. If n is greater
than w, it will cause an output error. If n is equal to w, it
will cause an output error only if the number is negative.
In an input statement, the I field descriptor transfers w
characters from an external field into an internal value.
The following rules apply (see Integer Input Processing):
- The external field must contain an integer constant, and
it must not have a decimal point or exponential notation.
- The characters in the external field must be right-
justified within the field, i.e., flushed to the right.
- The internal value is zero if the entire external field is
blank.
- Leading zeros or leading blank spaces in the external
field are ignored. Conversely, trailing zeros or trailing
blank spaces in the external field are taken as significant
zeros and are made part of the internal value.
Integer Input Processing
Field Descriptor |
External Field |
Internal Value |
I3
I5
I4
I6
I10 |
bb
12345
b-35
bb-20b
bbbb357246 |
0
12345
-35
-200
357246 |
Formatting of output is much more common than formating of input. Upon output, the I field descriptor transfers an internal value to an external field of w characters,
right-justified. The following rules apply (see Integer Output Processing):
If the internal value does not fill completely the external
field, leading blank spaces are inserted. If the internal
value does not fit within the external field because it is
too large, the external field overflows (a write overflow) and is entirely filled with asterisks (*).
If the internal value is negative, the external field must
be large enough to accomodate the negative sign.
If n is present, the external field consists of n digits (not
including the sign) and is filled with leading zeros if
necessary.
The following examples show integer output processing, from internal value to external field.
Integer Output Processing
Field Descriptor |
Internal Value |
External Field |
I3
I4
I5
I5
I6
I8
I5.1
I5.3
I5.5
I5.5 |
-123
-35
12345
135246
-200
-357246
0
32
4365
-789 |
***
b-35
12345
*****
bb-200
b-357246
bbbb0
bb032
04365
***** |
Example: Integer Field Descriptor
C234567890
READ(5,110) I,J,K
110 FORMAT(I3,I5,I8)
WRITE(6,120) I,J,K
120 FORMAT(I5,I8,I10) |
This example reads integers I, J, and K from a file connected to unit 5, according to format 110. Assuming that
the values are:
I= 5, J= 23, K= 367
The input (external field) should look like this:
bb5bbb23bbbbb367
The output (external field) is then written according to format 120, and would look like this:
bbb5bbbbbb23bbbbbbb367
Note that the first character of output was not printed.
This is because the first column of output is never printed,
and used instead for the control of carriage return. Specific
rules regarding carriage control will be introduced later in
this chapter.
Things to watch for in formatted input/output
of integer data:
With input, remember to right-justify the integer values in the external field. Gross errors
may occur if you do not follow this rule.
With output, make sure to specify an external
field large enough to accomodate the largest
number. Otherwise, an overflow will occur
and the output will instead contain a set of
asterisks.
|
Real Field Descriptor F
This field descriptor takes the form
Fw.d
where F is the format code for real values, w is the field
width, and d is the number of characters to the right of the
decimal point. For example:
F7.2
specifies a real format occupying up to 7 spaces, of which
2 are to the right of the decimal point.
The (optional) sign and decimal point in a real number
are counted as one space each. For example, -135.79
would occupy 7 spaces.
In an input statement, the F field descriptor transfers w
characters from an external field into an internal value.
The following rules apply (see Real Input Processing on
the next page):
- The internal value is positive if the first nonblank character of the external field is a plus sign, or if no sign appears.
The internal value is negative if the first nonblank character of the external field is a minus sign.
- The internal value is zero if the external field contains
only blank spaces or a decimal point (see Section A
below).
- If the external field includes a decimal point: (a) the location of that decimal point overrides the location
specified by the d; and (b) leading and/or trailing blanks are
filled with zeros and are not significant (see Section B
below).
- If the external field includes neither a decimal point nor
an exponent: (a) leading and/or trailing blanks are filled
with zeros; (b) the field is treated as a real value of w
characters; and (c) d is meaningful, with the rightmost d
digits taken as the fractional part of the real number (see
Section C below).
- If the external field includes an exponent, i.e., the letter
E followed by an optional exponent sign and number,
that exponent is used to establish the magnitude of the
internal value (see Section D below). In this case, the
characters in the external field should be right-justified.
The following examples illustrate real input processing
(transfer from external field to internal value).
Real Input Processing
Section
|
Field Descriptor
|
External Field
|
Internal Value
|
A | F5.1
F5.1 |
bbbbb
bb.bb |
0
0 |
B | F6.2
F8.3
F10.3 |
1.2345
12345.67
b54.3bbbbb |
1.2345
12345.67
54.3 |
C | F6.2
F6.2
F10.2
F10.2 |
123456
b1234b
bbb-12345.
-12345bbbb |
1234.56
123.40
-12345.
-1234500.00 |
D | F10.3
F13.6
F13.6
F13.6 |
bb1275E-01
bb123.456E-10
bbb1234567E-7
bb1234567E-07 |
1.275E-1
0.123456E-07
0.1234567E-06
0.1234567E-06 |
More commonly used is real output formatting, where
the F field descriptor transfers w characters from an internal value to an external field of w characters, rounded to d
decimal positions and right-justified. The following rules
apply:
If the internal value does not fill the entire external
field, leading blanks are inserted in the external field.
If the internal value does not fit within the external
field, the field overflows and is entirely filled with asterisks (*).
Note that the character field width w must exceed d by
at least 3 in order to fit in the (optional) sign, the decimal
point, and at least one digit to the left of the decimal point.
Real Output Processing
Field Descriptor
|
Internal Value |
External Field |
F4.1
F4.1
F5.1
F5.2
F8.3
F8.3
F8.5
F8.5
F10.4
F10.6
F10.8
|
15.632
1.63
-32.54
-32.54
1234.56
1234.567
12.34567
12345.67
-12.34567
-12.34567
-12.34567 |
15.6
b1.6
-32.5
*****
1234.560
1234.567
12.34567
********
bb-12.3457
-12.345670
********** |
Example: Real Field Descriptor
C234567890
READ(5,210) A,B
210 FORMAT(F10.3 F12,5)
WRITE(6,210) A,B
220 FORMAT(F12.3, F15.5) |
This example reads real variables A and B from a file
connected to unit 5, according to format 210. Assuming
that the values are:
A= 123.654, B= 45.89
The input (external field) should look like this:
123.654bbb45.89bbbbbbb
The output (external field) is then written according to format 220, and would look like this:
bbbb123.654bbbbbbb45.89000
Again, notice that the first character of output was not
printed (control of carriage return).
Things to keep in mind in formatted input/output of real data using the F field descriptor:
-
With input, always use a decimal point to ex
plicitly state its location and avoid having to
count columns when entering data.
-
With output, make the field width w large
enough to avoid a write overflow. If unsure of
how large, use the E field descriptor instead.
|
Real Field Descriptor E
This field descriptor takes the form
Ew.d
where E is the format code for exponential notation, w is
the field width, and d is the number of characters to the
right of the decimal point. For example:
E13.6
reserves 13 spaces, 6 of which are to the right of the decimal point. The signs, the letter E, and the decimal point
are counted as one character each (for example, the number -1.23E-02 takes up 9 spaces).
Input and output operations for the E field descriptor
work in the same way as the F field descriptor (Section A).
However, w must exceed d by at least 5 (but better yet 7,
see Section B) in order to fit, from left to right: the decimal point, the letter E, an exponent sign, a 2-digit exponent, an optional sign, and a possible zero to the left of the
decimal point (the latter may be omitted by the processor
if necessary to avoid a write overflow, see Section C).
Real Output Processing with E Field Descriptor
Field Descriptor
|
Internal Value >>>>>
|
External Field
|
A E10.2
E10.3
E12.7 |
0.246357
1234567.
-1234.567 |
bb0.25E+00
b0.123E+07
************ |
B
E12.5 |
-0.0001234 |
-0.12340E-03 |
C E12.6
E12.7 |
-1234.567
1234.567 |
-.123457E+04
.1234567E+04 |
Example: Real Field Descriptor E
C234567890
READ(5,310) C,D
310 FORMAT(E10.3,E15.5)
WRITE(6,320) C,D
320 FORMAT(E12.3,E15.5)
|
This example reads real variables C and D from a file
connected to unit 5, according to format 310. Assuming
that the values are:
C= -12.3, D= -45.89
The input (external field) should look like this:
-0.123E+02bbb-0.45890E+02
The output (external field) is then written according to format 320, and would look like this:
b-0.123E+02bbb-0.45890E+02
Again, notice that the first character of output was not
printed (control of carriage return).
Real Field Descriptor D
The D field descriptor is used for double-precision variables. This field descriptor takes the form
Dw.d
where D is the format code, w is the field width, and d is
the number of characters to the right of the decimal point.
For example:
D15.8
reserves 15 spaces, 8 of which are to the right of the decimal point. Input and output operations for the D field
descriptor work in the same way as the E field descriptor, except that the letter D appears instead.
Logical Field Descriptor L
This field descriptor takes the form
Lw
where L is the format code for logical values and w is the
field width.
For example:
L7
reserves 7 spaces.
In an input statement, the L field descriptor transfers w
characters from an external field into an internal value.
The stored value is either .TRUE. or .FALSE.
The following rules apply:
- The value is .TRUE. if the external field starts with a
T, or with a T preceded by a period (.T).
- The value is .FALSE. if the external field starts with
an F, with an F preceded by a period (.F), or is blank.
- If none of the above, an input error occurs during execution.
- If w= 1, the external values must be either T, F, or
blank.
- If w= 2, the external values must be either .T, .F, start
with a T or F, or be blank.
- If w is omitted, a value w=2 is assumed.
In an output statement, the L field descriptor transfers
the letter T if the internal value is .TRUE. or the letter
F if the internal value is .FALSE. The value is right-
justified in the external field. If w is omitted, a value is
assumed (usually w= 2).
The following examples illustrate logical input/output
processing.
Logical Input Processing
Field Descriptor |
External Value >>>>> |
Internal Field |
L5
L7
L9
L
L
L2
L2 |
.TRUE.
FRANKbb
bbbbbbbbb
T
b
TR
FL |
TRUE
FALSE
FALSE
TRUE
FALSE
TRUE
FALSE |
Logical Output Processing
Field Descriptor |
Internal Value >>>>> |
External Field |
L5
L7
L |
TRUE
FALSE
TRUE |
bbbbT
bbbbbbF
bT |
Example: Logical Field Descriptor L
C234567890
LOGICAL P,Q
READ(5,410) P,Q
410 FORMAT(L5,L5)
WRITE(6,420) P,Q
420 FORMAT(L3,L3) |
Assuming that P is .TRUE. and Q is .FALSE., the input
should look like this:
TRUEbFALSE
The corresponding output would be:
bTbbFb
Character Field Descriptor A
This field descriptor takes the form
Aw
where A is the character format code and w is the field
width.
For example:
A10
will allot 10 spaces for a character variable.
In an input statement, the A field descriptor transfers w
characters from an external field into an internal character
value of length v.
The character length v is specified by a CHARACTER
declaration statement. For example:
CHARACTER*12
where v = 12.
The following rules apply:
- If w is equal to v, w = v characters are transferred.
- If w is less than v, w characters are transferred, left-
justified, and as many trailing blanks are added as necessary to fill the length v.
- If w is greater than v, the rightmost v characters are
transferred and the leftmost excess characters are lost.
- If w is omitted, v characters are transferred. If the external value has z characters, where z < v, the internal field
is filled with z characters and v - z trailing blanks. If z >
w, the internal field is filled with the leftmost v characters.
Character Input Processing
Field Descriptor |
Character Length |
External Field >> |
Internal Value |
A6
A6
A8
A
A |
6
8
6
8
6 |
ABCDEF
ABCDEFGH
ABCDEFGH
ABCDEF
ABCDEFGH |
ABCDEF
ABCDEFbb
CDEFGH
ABCDEFbb
ABCDEF |
In an output statement, the A field descriptor transfers
w characters from an internal value of length v to an external field of length w. The following rules apply:
- If w is equal to v, w = v characters are transferred.
- If w is less than v, only the leftmost w characters are
transferred.
- If w is greater than v, v characters are transferred, right-
justified, with leading blank spaces.
- If w is omitted, v characters are transferred. If the inter
nal value has z characters, where z < v, the external field
is filled with z characters and v - z trailing blanks. If z >
v, the external field is filled with the leftmost v characters.
Character Output Processing
Field Descriptor |
Character Length |
External Field >> |
Internal Value |
A6
A6
A8
A
A |
6
8
6
8
6 |
ABCDEF
ABCDEFGH
ABCDEF
ABCDEF
ABCDEFGH |
ABCDEF
ABCDEF
bbABCDEF
ABCDEFbb
ABCDEF
|
Example: Character Field Descriptor A
C234567890
CHARACTER WORD*10,LABEL*12,NAME*15
READ(5,510) WORD,LABEL,NAME
510 FORMAT(A10,A12,A)
WRITE(6,520) WORD,LABEL,NAME
520 FORMAT(1X,A10,A12,A) |
This example reads real variables WORD, LABEL,
and NAME from a file connected to unit 5, according to
format 510. Assuming that the values are:
WORD= 'YELLOW', LABEL= 'TURQUOISE',
NAME= 'KIRKPATRICK'
The input (external field) should look like this:
YELLOWbbbbTURQUOISEbbbKIRKPATRICKbbbb
The output (external field) is then written according to for
mat 520, and would look like this:
YELLOWbbbbTURQUOISEbbbKIRKPATRICKbbbb
In this example, a 1X edit descriptor (See Edit Descriptor X and Carriage Control later in this section) is included as the first edit descriptor listed in the output FORMAT statement (with label 520). If the 1X is omitted, the
first character of variable WORD (in this case, Y) is used
for carriage control and is not printed.
Things to keep in mind in formatted input/output
of character data:
-
Try to match the field width w with the character length v to avoid loss of data or added blank
spaces.
-
Consider omitting the field width entirely. In
this case, the specified character length will be
used as field width.
|
Character Field Descriptor H
This field descriptor takes the form
nHabc & def
where H is the format code and n specifies the exact number of characters to be transferred, to follow immediately
after the H code (in this case, the characters abc & def).
Leading, embedded, and trailing blank spaces are included
in the n count.
For example:
17H VELOCITY PROFILE
will transfer 17 characters, including blanks.
A limitation of the H field descriptor is that the number
of characters following H has to equal n exactly. Beginning with Fortran 77, there are other more convenient
ways of writing character constants, which avoid the
counting of characters. Therefore, the H field descriptor
has been categorized as obsolescent by the Fortran 90
standard.
As an alternative to the A and H field descriptors, character constants may be specified directly; either leading,
trailing, or embedded within (1) variable lists in WRITE
statements, or (2) field and edit descriptor lists in FORMAT statements (See Example on next page).
In most of these examples, a 1X edit descriptor is used
to account for carriage control (See Edit Descriptor X and
Carriage Control later in this section). If 1X is omitted (as
in format 620), a blank space leading the character constant can be used for carriage control (' THE VALUE OF
B IS = ').
Example: Use of Character Constants
C234567890
WRITE(6,610) A
610 FORMAT(1X,20HTHE VALUE OF A IS = ,F10.3)
WRITE(6,620) ' THE VALUE OF B IS = ',B
620 FORMAT(F10.3)
WRITE(6,630)'THE VALUE OF C IS = ',C,' UNITS'
630 FORMAT(1X,A,F10.3,A)
WRITE(6,640) D
640 FORMAT(1X,'THE VALUE OF D IS = ',F10.5,
1' UNITS')
WRITE(6,650) Y,Z
650 FORMAT(1X,'Y= ',F10.3/1X,'Z= ',F10.5) |
Assuming A = -23.4562, B = 123456.789, C= 75.9,
D= -123.45678, Y= -12345.032, and Z = 9876.5432;
the output would look like this:
THE VALUE OF A IS = -23.456
THE VALUE OF B IS = 123456.789
THE VALUE OF C IS = 75.900 UNITS
THE VALUE OF D IS = -123.45678 UNITS
Y= -12345.032
Z= 9876.54321
Edit Descriptor X
This edit descriptor takes the form
nX
where X is the format code and n specifies the number of
characters that are passed over, or jumped. The value of n
must be greater than or equal to 1. If n= 1, it may be optional in some processors. However, to ensure compatibility among processors, it is good practice to always precede
the X code with a number, even if it happens to be 1.
Several X edit descriptors within one format specification result in the skipping of a combined number of
spaces, as in the following example:
20 FORMAT(1X,'LENGTH',5X,'WIDTH',5X,'HEIGHT')
which results in
LENGTHbbbbbWIDTHbbbbbHEIGHT
Edit Descriptor T
This edit descriptor takes the form
Tn
where T is the format code and n specifies the number of
characters that are tabulated; n must be greater than or
equal to 1. In an input statement, the first n characters of
the record are skipped. In an output statement, Tn tabs to
column n of the record (column n-1 of the printed output,
since the first column of the record is never printed).
Several T edit descriptors within one format specification result in positional tabulation, each time from the
beginning of the record, as in the following example:
20 FORMAT(T21,'COLUMN 2',T11,'COLUMN 1')
which results in
C23456789012345678901234567890
COLUMN 1 COLUMN 2
In this example, the character constant COLUMN 1 is
printed starting in column 11 of the record (column 10 of
the printed output, since the first column of the record is
never printed). The character constant COLUMN 2 is
printed starting in column 21 of the record (column 20 of
the printed output).
Edit Descriptor $
The edit descriptor $ (optional with some processors)
modifies the carriage control specified by the first character of the record. This descriptor can be placed anywhere
in the list of descriptors. If more than one $ sign appears,
the first one is used and the others are ignored.
In an input format, the edit descriptor $ is ignored. In
an output format, the $ descriptor suppresses the carriage
return if the first character of the record is a blank space.
This feature is convenient when using direct screen input
(interactive input), because the input data record can be
entered on the same line as the input prompt. This improves the readability of the input data being entered to the
screen.
Example: Edit Descriptor $
C234567890
WRITE(6,700)
700 FORMAT(1X,'ENTER VALUE OF A: ',$)
READ(5,*) A |
This example allows the value of A to be typed on the
same line as the input prompt. As the input prompt is
ENTER VALUE OF A:
the value of A, say 12.3, is to be entered to the screen immediately following the input prompt. The prompt and in
put line on the screen would look like this:
ENTER VALUE OF A: 12.3
Carriage Control
With output, the first character of a record is reserved to
specify the number of carriage returns (carriage control).
Table 6.2 lists valid carriage control characters; b stands
for a blank space or for any other character not listed in
this table. If a field descriptor for carriage control is omitted, either by design or involuntarily, the first character of
the record is used for carriage control. If this happens, that
character is taken over by the processor and not printed.
TABLE 6.2
CARRIAGE CONTROL CHARACTERS
Character
|
No. of Returns
|
Meaning
|
b
0
1
+
$
|
1
2
several
0
0
|
Go to next line (single spacing)
Skip one line (double spacing)
Skip to top of next page (paging)
Go to the beginning of the same
line (overwrite previous record)
Suppress
single-spacing carriage
return (remain at the end of the line). |
It is good programming practice to explicitly account
for the intended number of carriage returns at the beginning of each output format. Otherwise, there is the possibility of unpredictable output. For instance, if the first
character of every line happens to be a 1, one page will be
used per line of output.
Example: Carriage Control
C234567890
WRITE(6,810) A
WRITE(6,820) B
WRITE(6,830)
WRITE(6,840)
WRITE(6,850)
WRITE(6,860)
WRITE(6,870)
810 FORMAT(1X,'THE VALUE OF A IS = ',F10.3)
820 FORMAT(' THE VALUE OF B IS = ',F10.3)
830 FORMAT(' ','GO TO NEXT LINE, SINGLE SPACING')
840 FORMAT('-','GO TO NEXT LINE, SINGLE SPACING')
850 FORMAT('0','SKIP ONE LINE, DOUBLE SPACING')
860 FORMAT('1','SKIP TO TOP OF NEXT PAGE')
870 FORMAT('+','GO TO BEGINNING OF THE SAME LINE.',
1'WRITE OVER PREVIOUS RECORD') |
Repeat Count of Field Descriptors
The field descriptors I, F, E, D, L, and A can be repeated
within one record by preceding any of them by an integer,
intended as a repeat count.
For example, the following formats are equivalent:
10 FORMAT(I5,I5,F10.4,F10.4,F10.4)
20 FORMAT(2I5,3F10.4)
Several field descriptors can be repeated as a group, by
enclosing the list in parentheses and preceding it by an
integer, intended as a group repeat count. If a group re
peat count is omitted, a value of 1 is assumed.
These formats are equivalent:
30 FORMAT(1X,I3,E12.5,I3,E12.5/)
40 FORMAT(1X,2(I3,E12.5)/)
Output Statement Record Overflow
With output, up to 133 columns can be specified per
record, including the first column, which is reserved for
carriage control. For example, these two statements are
equivalent ways to print a series of 132 dashes:
WRITE(6,100)
100 FORMAT(133(H-))
WRITE(6,200)
200 FORMAT(1X,132(H-))
On the other hand, the following format statement will
cause a record overflow (an execution error):
100 FORMAT(1X,133(H-))
This format specifies 134 columns, one more than the
maximum of 133 allowed.
6.3 OTHER FORMAT TOPICS
Interaction of Input/Output Statements with
Format Statements
In Fortran, the input/output (READ and WRITE) and
format statements interact in a specific way to allow the
orderly input/output of a list of variables. This interaction
is described in this section.
There is a close relationship between input/output
variable lists and their respective field descriptor lists
specified in the format. The basic concept to remember is
that control remains with the READ or WRITE statement.
A READ or WRITE statement executes the transfer of a
number of data items equal to the number of variables
listed in the statement.
-
If the field descriptor list is longer than the variable
list, the latter takes precedence. From left to right, the
processor uses only as many field descriptors as needed
to accomplish the transfer of all items listed on the variable list. The remaining field descriptors are ignored.
-
If the field descriptor list is shorter than the variable
list, the processor treats the format specification as typical, and repeats it as many times as needed to accomplish the transfer of all items on the variable list. During
the repeated execution of the field descriptor list, the
processor ignores any remaining field descriptors.
The following examples illustrate the interaction of input/output and format statements.
Example 1
READ(5,100) A,B,I,J
100 FORMAT(F10.3,F10.4,I5,I10)
The variable list in the READ statement matches in
number and type the field descriptor list in the FORMAT
statement. Two real and two integer values will be read.
Example 2
READ(5,200) A,B,C,D
200 FORMAT(F10.3,F10.4,F10.5,F10.6)
The variable list matches in number and type the corresponding field descriptor list. Four real values (A, B, C,
and D) will be read in sequence.
Example 3
READ(5,300) A,B
300 FORMAT(F10.3,F10.4,F10.5,F10.6)
The variable list is shorter than the corresponding field
descriptor list. Only the first two field descriptors will be
used, and two real values (A and B) will be read.
Example 4
WRITE(6,400) A,B,C,D
400 FORMAT(1X,F10.3)
The variable list is longer than the corresponding field
descriptor list. The format is interpreted as typical. All
four variables will be written, one per line, following the
field descriptor specified in the format. The output may
look like this:
12345.678
-98.765
-0.345
-98765.432
Example 5
WRITE(6,500) A,B,C,D
500 FORMAT(1X,F10.3,F10.4/1X,F10.5,F10.6)
The variable list matches the corresponding field
descriptor list in number and type. The slash in the format
indicates that two records (lines) will be written. In total,
four real values will be written (A, B, C, and D), two per
line. The output may look like this:
12345.678 -98.7654
-0.34543 -8.112345
Example 6
WRITE(6,600) A,B,C
600 FORMAT(1X,F10.3/1X,F10.4/1X,F10.5,F10.6)
The variable list is shorter than the corresponding field
descriptor list. Only the first three field descriptors will be
executed. Their types match those of the variable list.
The presence of two slashes in the format indicates that
three records (lines) will be written. In total, three real values will be written, one per line. The output may look like
this:
12345.678
-98.7654
-0.34543
Example 7
WRITE(6,700) A,B,C,D,E,F,G
700 FORMAT(F10.3/F10.4/F10.5,F10.6)
The variable list is longer than the corresponding field
descriptor list. The format is interpreted as typical. All
seven variables will be written following the typical format. The second time, only the first three field descriptors
will be executed. Their types match those of the variable
list. The presence of two slashes in the format indicates
that three records will be written in the first pass, and three
on the second. In total, seven real values (A, B, C, D, E, F,
and G) will be written from six records (A in the first record, B in the second, C and D in the third, E in the fourth,
F in the fifth, and G in the sixth).
The output may look like this:
12345.789
9876.5432
-106.34521 -2.987654
-14567.897
-2230.6784
9876.43245
Things to keep in mind when matching
variable lists and field descriptor lists:
-
Avoid using mixed-type variable and field
descriptor lists, particularly with long integer
and real lists.
-
If you use mixed-type lists, make sure that the
listed items agree in number and have match
ing data types. Any mismatch will cause an
execution error. This is a very common
source of error.
-
Single-type variable and field descriptor lists
may disagree in number of elements. How
ever, this will either disregard a part of the
format, or will transfer more records than the
number specified in the format.
|
Runtime Formats
Some processors allow the READ or WRITE statement to
vary its format during program execution. This is referred
to as a "runtime format." For this purpose, a character constant or character variable is used in a READ or WRITE
statement instead of the fixed format statement label.
When using a character variable in a runtime format, make
sure that it is defined in length and value prior to the invoking READ or WRITE statement.
A runtime format frees the input/output statement from
having to refer to a format label. In certain cases, the format statement may be somewhere else within the program
unit, far from the invoking READ or WRITE statement.
The following examples illustrate runtime formats. For
comparison, Example 1 shows the conventional paired
WRITE and FORMAT statements, where the format is
fixed. Example 2 uses a character constant containing the
appropriate format specification. Example 3 defines a
character variable FMT and uses it in the FORMAT statement. All three examples are equivalent, and cause the
same variables to be output in exactly the same way.
Example 1: Conventional Fixed Format
WRITE(6,100) A,B,C
100 FORMAT(1X,3F10.3)
Example 2: Runtime Format (Character Constant)
WRITE(6,'(1X,3F10.3)') A,B,C
Example 3: Runtime Format (Character Variable)
CHARACTER*11 FMT
DATA FMT /'(1X,3F10.3)'/
WRITE(6,FMT) A,B,C
Variable Formats
Some processors allow a WRITE statement to vary its
format specification during program execution. This is referred to as a "variable format." For this purpose, a valid
arithmetic integer expression, enclosed in angle brackets
<>, is used to replace an integer constant wherever it is
used as part of a format specification.
Example Program: Variable Formatting
C234567890
PROGRAM VAR_FOR
DATA J,K,L,M,N /3,5,7,9,0/
10 N= N+1
IF(N.LE.8) THEN
WRITE(6,100) J,K,L,M
100 FORMAT(1X,4I<N+5>)
GO TO 10
ENDIF
END |
In this program:
-
The digits 3, 5, 7, 9, and 0 have been initialized in a
DATA statement.
-
A logical IF/GO TO combination results in the execution of a WRITE statement 8 times.
-
The character field width of format 100 is incremented
by one (1) every time the WRITE statement is executed.
The output would look like this:
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
3 5 7 9
6.4 EXAMPLE PROGRAMS
Example Program 1: Table of Trigonometric Functions
C234567890
PROGRAM TRIG_FUNCTIONS
CHARACTER*11
1ANGLE*7,SINE,COSINE,TANGENT,COTANGENT,SECANT,COSECANT,INFINITY
CHARACTER*19 LINE
DATA PI,IANGLE /3.1415926,-5/
DATA
1ANGLE,SINE,COSINE,TANGENT,COTANGENT,SECANT,COSECANT,INFINITY
2 /' ANGLE', ' SINE',' COSINE',' TANGENT',
3 ' COTANGENT',' SECANT',' COSECANT',' INFINITY'/
DATA LINE /'-------------------'/
WRITE(6,500) LINE,LINE,LINE,LINE
WRITE(6,100) ANGLE,SINE,COSINE,TANGENT,COTANGENT,SECANT,COSECANT
WRITE(6,500) LINE,LINE,LINE,LINE
20 CONTINUE
IANGLE= IANGLE + 5
X= FLOAT(IANGLE)*PI/180.
SINX= SIN(X)
COSX= COS(X)
IF(IANGLE.EQ.0) THEN
TANX= TAN(X)
SECX= 1/COSX
WRITE(6,200) IANGLE,SINX,COSX,TANX,INFINITY,SECX,INFINITY
ELSEIF(IANGLE.LT.90) THEN
TANX= TAN(X)
COTX= 1/TANX
SECX= 1/COSX
CSCX= 1/SINX
WRITE(6,300) IANGLE,SINX,COSX,TANX,COTX,SECX,CSCX
ELSEIF(IANGLE.EQ.90) THEN
COTX= 1/TANX
CSCX= 1/SINX
WRITE(6,400) IANGLE,SINX,COSX,INFINITY,COTX,INFINITY,CSCX
ELSE
WRITE(6,500) LINE,LINE,LINE,LINE
STOP
ENDIF
GO TO 20
100 FORMAT(1X,A,1X,6A)
200 FORMAT(1X,I5,3X,3F11.6,A,F11.6,A)
300 FORMAT(1X,I5,3X,6F11.6)
400 FORMAT(1X,I5,3X,2F11.6,A,F11.6,A,F11.6)
500 FORMAT(1X,4A)
END
|
This program shows how to calculate and print a table
of values. The program calculates and prints all six trigonometric functions for angles varying between 0o and 90o,
at increments of 5o.
Note the following features of this program:
-
The integer IANGLE has been initialized at -5 to enable
the calculation of the angle to start at 0o.
-
The block IF construct allows the computation and
printing of the special cases (0o and 90o).
-
The character constant INFINITY is printed where the
value of the function is ∞.
-
The character constant LINE has been used to draw
separating lines.
The output of program TRIG_FUNCTIONS is shown
below.
-------------------------------------------------------------------------
ANGLE
SINE COSINE TANGENT
COTANGENT SECANT COSECANT
-------------------------------------------------------------------------
0 0.000000
1.000000 0.000000
INFINITY 1.000000
INFINITY
5 0.087156
0.996195 0.087489 11.430053 1.003820 11.473714
10
0.173648 0.984808 0.176327
5.671281 1.015427
5.758770
15 0.258819
0.965926 0.267949
3.732051 1.035276
3.863704
20 0.342020
0.939693 0.363970
2.747478 1.064178
2.923805
25
0.422618 0.906308 0.466308
2.144507 1.103378
2.366202
30 0.500000
0.866025 0.577350
1.732051 1.154701
2.000000
35 0.573576
0.819152 0.700207
1.428148 1.220775
1.743447
40 0.642788
0.766044 0.839100
1.191754 1.305407
1.555724
45 0.707107
0.707107 1.000000
1.000000 1.414213
1.414214
50
0.766044 0.642788 1.191753
0.839100
1.555724 1.305407
55 0.819152
0.573577 1.428148
0.700208
1.743447 1.220775
60
0.866025 0.500000 1.732051
0.577350
2.000000 1.154701
65 0.906308
0.422618 2.144506
0.466308
2.366201 1.103378
70
0.939693 0.342020 2.747476
0.363970
2.923803 1.064178
75 0.965926
0.258819 3.732049
0.267949
3.863701 1.035276
80 0.984808
0.173648 5.671280
0.176327
5.758769 1.015427
85 0.996195
0.087156 11.430044
0.087489 11.473705
1.003820
90
1.000000 0.000000 INFINITY
0.087489 INFINITY
1.000000
-------------------------------------------------------------------------
Example Program 2: Variable Formats
C234567890
CHARACTER X2*2
DATA L,X2 /0,'XX'/
10 L= L+1
IF(L.LE.11) THEN
WRITE(6,100) X2,X2
GO TO 10
ENDIF
100 FORMAT
*(<1+ABS(5-ABS(6-L))>X,A,<ABS(2*(6-L))>X,A)
END |
This program uses a variable format to print the character pattern shown below. (Note that your compiler/processor may not have this feature).
XX XX
XX XX
XX XX
XX XX
XX XX
XXXX
XX XX
XX XX
XX XX
XX XX
XX XX
The features of this program are:
-
L varies from L = 1 to L = 11. One line is printed per
value of L. L = 1 for line 1; L = 2 for line 2; and so on.
-
The intrinsic function ABS (see Section 5.4) is used to
calculate the variable number of blank spaces to be inserted between the XX's.
-
The carriage control character (1X) has been incorporated into the first field descriptor listed in Format 100.
6.5 SUMMARY
This chapter describes formatted input/output by linking a
FORMAT statement to a READ or WRITE statement.
The FORMAT statement contains field and edit descriptors. The field descriptors I, F, E, D, L, A, and H specify
the size and form of the data item. The edit descriptors X,
T, and $ affect the position or location of the data item.
Format codes are used to specify field and edit descriptors.
READ or WRITE statements interact with FORMAT
statements in a specific way to allow the orderly input/output of a long list of variables.
CHAPTER 6--PROBLEMS
-
Write a program to calculate and print a table of hyperbolic trigonometric functions, for angles between 0 and 6 radians, in increments of 0.1
radians. Use six decimal figures.
-
Write a program to calculate and print a table of decimal logarithms, for
values ranging from 1.00 to 9.99, in increments of 0.01. Arrange the table in double-entry form, as follows:
x 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09
_________________________________________________________________
1.0
1.1
2.0
3.0
etc.
_________________________________________________________________
Use four decimal figures.
-
Write a program to calculate and print a table of natural logarithms, for
values ranging from 1.00 to 9.99, in increments of 0.01. Arrange the table in double-entry form (see Problem 2). Use four decimal figures.
-
Do Problem 4.4 (baggage handling) using formatted input/output.
-
Do Problem 4.5 (water utility billing) using formatted input/output. Set
output in tabular form, in four columns labeled NAME, ADDRESS,
CONSUMPTION, and BILLING.
-
Do Problem 4.10 (electric utility billing) using formatted input/output.
Set output in tabular form, in four columns labeled NAME, ADDRESS,
CONSUMPTION, and BILLING.
-
Write a program to calculate and print a table of the hyperbolic sine, for
values ranging from 0.00 to 5.99, in increments of 0.01. Arrange the table in double-entry form (see Problem 2). Use two decimal figures.
-
Write a program to calculate and print a table of the hyperbolic cosine,
for values ranging from 0.00 to 5.99, in increments of 0.01. Arrange the
table in double-entry form (see Problem 2). Use two decimal figures.
-
Write a program to calculate and print a two-column table. The
left column should have integer N, varying from 0 to 30, and the right column
its factorial. Use the E field descriptor to print the factorial.
-
Write a program to calculate and print a two-column table. The left column should have integer N, varying from 1 to 100, and the right column
its cubic root, with 6 decimal figures.
-
Write a program to calculate and print a two-column table. The
left column should show ANGLE (RADIANS), varying from 0 to 10, in inter
vals of 0.1 radians. The right column should show ANGLE (DEGREES,
MINUTES, SECONDS), the latter with 1 decimal figure, as follows:
6.7 radians (corresponding to) 383o 52' 54.2"
Use double-precision variables.
-
Write a program to print the following design using a variable format:
X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X X X X X
X X X X
-
Write a program to print the following diamond design using a variable
format:
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
DD DD DD DD
-
Write a program to print the following 3 letters (XYZ) using a variable
format (as much as possible) and 5 blank spaces between letters.
X X Y Y ZZZZZZZZZZZ
X X Y Y Z
X X Y Y Z
X X Y Y Z
X X Y Y Z
X Y Z
X X Y Z
X X Y Z
X X Y Z
X X Y Z
X X Y ZZZZZZZZZZZ
|