5.input Output
5.input Output
5.input Output
• If I/O unit i is not associated with any device or file, fort.i will be the external filename.
Example:
program test_io_unit
implicit none
real :: pi = 3.14159265
write(*,*) 'I/O unit=*', pi
write(0,*) 'I/O unit=0', pi
write(6,*) 'I/O unit=6', pi
write(91,*) 'I/O unit=91', pi
write(2147483647,*) 'I/O unit=2147483647', pi
end program
Output on screen:
I/O unit=* 3.1415927
I/O unit=0 3.1415927
I/O unit=6 3.1415927
fort.2147483647
I/O unit=2147483647 3.1415927
Formats and Formatted WRITE statements
Output on screen:
The result for iteration 21 is 3.141593
----+----+----+----+----+----+----+---------+----+----+----+
5 10 15 20 25 30 35 40 45 50 55 60
• Formatted WRITE
write(*,100) iter, result
100 format (' The result for iteration ', I3, ' is ', F7.3)
Output on screen:
The result for iteration 21 is 3.141
----+----+----+----+----+----+----+---------+----+----+----+
5 10 15 20 25 30 35 40 45 50 55 60
I3 F7.3
format descriptors for the
integer variable iter and real variable result
• Format in FORMAT statement :
write(*,120) ' The result for iteration ', iter, ' is ', result
120 format (A26,I3,A4,F7.3)
character(len=20) :: string
string = '(A26,I3,A4,F7.3)'
write(*,string) ' The result for iteration ', iter, ' is ', result
Format Descriptors
• I descriptor
rIw[.m] r: repeat count
w: field width
m: minimum number of digits to be displayed
Example:
integer :: m = -12, n = 0, k = -12345
write(*,200) m, n, k
200 format(2I5, I10)
write(*,201) m, n, k
201 format(2I5.0, I10.8)
write(*,202) m, n, k
202 format(2I5.3, I5)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
- 1 2 0 - 1 2 3 4 5
- 1 2 - 0 0 0 1 2 3 4 5
- 0 1 2 0 0 0 * * * * *
• F descriptor
rFw.d r: repeat count
w: field width
d: number of digits to right of decimal place
Example:
real :: pi = 3.141593
write(*,101) pi, pi
101 format(F7.3, F10.8)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
3 . 1 4 2 3 . 1 4 1 5 9 3 0 0
Example:
real :: a = -12.3, b = .123, c = 123.456
write(*,200) a, b, c
200 format(3F10.3)
write(*,201) a, b, c
201 format(3F10.2)
write(*,202) a, b, c
202 format(2F6.3, F8.3)
5 0 5 0 5 0
- 1 2 . 3 0 0 0 . 1 2 3 1 2 3 . 4 5 6
- 1 2 . 3 0 0 . 1 2 1 2 3 . 4 6
* * * * * * 0 . 1 2 3 1 2 3 . 4 5 6
• E descriptor
rEw.d r: repeat count
w: field width
d: number of digits to right of decimal place
±0.ddddE±ee (w ≥ d+7)
Example:
a = 1.2346E6
b = 0.001
c = -12.3E10
write(*,'(E14.4)') a
write(*,'(E14.4)') b
write(*,'(E13.6)') c
write(*,'(E11.6)') c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
0 . 1 2 3 5 E + 0 7
0 . 1 0 0 0 E - 0 2
- 0 . 1 2 3 0 0 0 E + 1 2
* * * * * * * * * * *
• ES descriptor (scientific notation)
rESw.d r: repeat count
w: field width
d: number of digits to right of decimal place
±x.ddddE±ee (w ≥ d+7)
Example:
a = 1.2346E6
b = 0.009
c = -12.3E10
write(*,'(ES14.4)') a
write(*,'(ES14.4)') b
write(*,'(ES12.6)') c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 . 2 3 4 6 E + 0 6
9 . 0 0 0 0 E - 0 3
* * * * * * * * * * * *
• A descriptor (for character I/O)
rA[w] r: repeat count
w: the width of field (is the same as the number of characters)
Example:
character(len=17) :: string = 'This is a string.'
write(*,'(A)') string
write(*,'(A20)') string
writ(*,'(A6)') string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
T h i s i s a s t r i n g .
T h i s i s a s t r i n g .
T h i s i
• X and T descriptor (for horizontal position)
nX n: number of blanks to insert
Tc c: column number to go to
Example:
character(len=10) :: first_name = 'James '
character :: initial = 'R'
character(len=16) :: last_name = 'Johnson '
character(len=9) :: class = 'ESOE 2015'
integer :: grade = 92
write(*,100) first_name, initial, last_name, grade, class
100 format(A10, 1X, A1, 1X, A10, 4X, I3, T50, A9)
A10 1X 1X A10 4X I3 A9
A1 T50
Notes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
4 5
4
5
5a. More on
Input and Output
Data Files and File processing
READ(i,j)
WRITE(i,j)
• To use external file with name different from fort.i, we need to select the data
file and to read from or write to the file.
These statements include:
▫ OPEN: associate a file with a I/O unit number
▫ CLOSE: end the association of a file with the I/O unit number
▫ REWIND: move to the beginning of a file
▫ BACKSPACE: move back one record in a file
• OPEN & CLOSE statements
open a file for input:
Ex: integer :: ierror
real :: a(10)
open(unit=8, file='input.dat', status='old', &
& action='read', iostat=ierror)
read(8,*) (a(i), i = 1,10) ! read a(1) to a(10) from input.dat
...
...
close(unit=8)
• unit=int_expr
to indicate the I/O unit number
• file=char_expr1
to specify the filename to be input/output
• status=char_expr2
char_expr2 is one of 'OLD', 'NEW', 'REPLACE', 'SCRATCH', 'UNKNOW'
• action=char_expr3
char_expr3 is one of 'READ', 'WRITE', 'READWRITE'
• iostat=int_var
if the open statement execute successfully, 0 is assigned to int_var,
otherwise position number is assigned.
Ex: open(unit=8, file='input.dat', status='old', &
& action='read', iostat=ierror)
。 iostat=int_var is not present in READ, the program will abort on an attempt to read
beyond the end of a file.
。 iostat=int_var is present in READ:
∙ if the READ statement is successfully , a 0 is assigned to the variable int_var;
∙ if read beyond the end of a file, a negative number is assigned and the program continues ;
∙ if file or format error occurs, a positive number is assigned and the program continues .
ELSE fileopen
END IF fileopen
X.0 X.1 X.2 X.3 X.4 X.5 X.6 X.7 X.8 X.9
----------------------------------------------------------------
1.0 0.000 0.041 0.079 0.114 0.146 0.176 0.204 0.230 0.255 0.279
3.0 ...
4.0 ...
5.0 ...
6.0 ...
7.0 ...
8.0 ...
9.0 ...
10.0 ...