2. Input/Output
There are two types of input/output
statements in Fortran:
List directed: format of input or output is
automatically supplied by the compiler.
Programmer-Formatted: format of input or
output is supplied by the programmer.
3. Formatted Output
There are two output statements in
Fortran, the print statement and the
write statement.
Print:
print format specifier, output-list
Write:
write (control_list), output-list
4. Format Specifier
Format specifier is one of the following:
* (an asterisk)
A character constant or a character
variable whose value specifies the form of
the output.
The label of a FORMAT statement.
5.
* indicates a list directed output.
As for the character constant or variable, the
formatting information is given the form:
‘(list of format descriptors)’
or
“(list of format descriptors)”
Ex: print ‘(1X, I5, F8.2)’, Number,Temperature
6.
The formatting information may be supplied
by a format statement whose statement
number is specified.
form
label format(list of formal descriptors)
where label is an integer in the range 1
through 99999.
Ex:
print 20, Number,temperature
20 format(1X, I5,F8.2)
7.
1X, I5, F8.2 are format descriptors that
specify the format in which the values of
number and temperature are to be
displayed.
8. Control Characters
Some compilers use the first character of
each line of output to control vertical spacing.
Control Character
Effect
.
blank
Normal spacing, advances to the
next line before printing
0
Double spacing, skip one line before printing
1
Advance to top of next page before printing
+
Overprint the last line printed
9.
Print ‘(I3)’, N
I3 indicates that the value to be printed is an
integer, and is to be printed on the first three
positions of a line.
In N=15 the three position are filled with b15.
So it will be interpreted as normal spacing
and print on the first 2 positions of the next
line 15
--
10.
If N=150 the first three positions will be
150. The 1 in the first position is
removed and interpreted as a control
character. So it will print 50 at the top of
the new page.
11.
To avoid confusion, we can explicitly indicate
for each output line what printer control is
desired.
1X or “ “ normal spacing
“0”
for double spacing
“1”
for advancing to a new page
“+”
for overprinting.
Note: in all of the following examples, 1X will
be used at the beginning of each output
format specifier.
12. Integer Output (I Descriptor)
rIw or rIw.m
I: denotes integer data.
w: integer constant indicating the width
of the field in which the data is to be
displayed.
r: integer constant. Repetition indicator.
m: minimum number of digits to be
displayed.
13. Ex:
Integer:: number=3, L=5378,k=-12345
print ’(1X,2I5,I7,I10)’,number,number-3,L,kappa
Or
print 30, number,number-3, L, kappa
30 format (1X,2I5,I7,I10)
_____3____0___5378____-12345
numbers will be right justified.
15. Real Output
rFw.d
F: denotes real (floating point) data.
w: integer constant indicating the total width
of the field in which the data is to be
displayed.
d: integer constant indicating the number of
digits to the right of the decimal point
r: integer constant. Repetition indicator.
16. Ex:
Integer:: in=625, out = -19
real:: a= 7.5, b=0.182, c=625.327
print 55, in,out,a,b,c
55 format (1X,2I4,2f6.3,f8.3)
__625_-19_7.500_0.182_625.327
18. Real Output
rEw.d
or rEw.dEe
E: indicates that the data is to be output in
exponential notation.
w: integer constant indicating the total width
of the field in which the data is to be
displayed.
d: integer constant indicating the number of
digits to the right of the decimal point
r: integer constant. Repetition indicator.
e: number of positions for displaying the
exponent.
19. Character Output
Character constants may be displayed by
including them in the list of descriptors of a
format specifier.
Real:: a = 0.3, b = 7.9
print ’(1X,”a=“,F6.2,” b=“,F6.2)’,a,b
_a=__0.30_b=__7.90
print ’(1X,A,F6.2,A,F6.2)’,”a=“,a,” b=“,b
will produce the same output.
20. Positional Descriptors (X and T)
Two format descriptors can be used to
provide spacing in an output line:
X: inserts blanks on an output line
form nX where n is the # of blanks
T: similar to the tab key
form Tc where c is an integer denoting the
number of a space on a line at which a field is
to begin.
22. Repeating groups of format descriptors
3F10.2 is eqv. to F10.2,F10.2,F10.2
‘(1X,A,F6.2,A,F6.2)’ is eqv to
‘(1X,2(A, F6.2))’
‘(1X,F18.2,I3,A,I3,A, F18.2,I3,A,I3,A,F8.4)’
‘(1X,2(F18.2,2(I3,A,)),F8.4)’
23. The slash (/) descriptor
The slash causes the output to begin on
a new line.
It can be used with a repetition indicator
to skip several lines.