5.input Output

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

5.

Input and Output


File Input and Output
read(i,j)
write(i,j)

• j is the statement number of format statement.

• i is the I/O unit or logical unit associated with device or file.


non-negative integer from 0 to 2147483647 (the maximum 4-byte integer value).

• Usually, I/O unit 5 is reserved for default input device,


6 is reserved for default output device,
0 is reserved for error display.

• 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

Output to two files:


fort.91
I/O unit=91 3.1415927

fort.2147483647
I/O unit=2147483647 3.1415927
Formats and Formatted WRITE statements

• Free format WRITE


write(*,*) 'The result for iteration ', iter, ' is ', result

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)

write(*,110) iter, result


110 format (' The result for iteration ', I3, ' is ', F7.3)

• Format in character constant :


write(*,'(A26,I3,A4,F7.3)') &
' The result for iteration ', iter, ' is ', result

• Format in character variable :

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)

James R Johnson 92 ESOE 2015


----+----+----+----+----+----+----+----+----+----+----+----+
5 10 15 20 25 30 35 40 45 50 55 60

A10 1X 1X A10 4X I3 A9
A1 T50
Notes:

• Repeating groups of format descriptors


320 format(1X, I6, I6, F10.2, F10.2, I6, F10.2, F10.2)

320 format(1X, I6, 2(I6, 2F10.2))

• Changing output lines: "/" descriptor


i = 4
j = 5
write(*,'(I3,I3)') i, j
write(*,'(I3/I3)') i, j

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)

• j is the statement number of format statement

• i is the I/O unit or logical unit associated with device or file.

• Usually, I/O unit 5 is reserved for default input device,


6 is reserved for default output device,
0 is reserved for error display.
• If the I/O unit i is not associated with any device or file,
fort.i will be the external filename.

• 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)

open a file for output:


Ex: integer :: ierror, n_unit=9
real :: a(10)
character(len=7) :: data_name='OUT.DAT'
open(unit=n_unit, file=data_name, status='NEW', &
& action='WRITE', iostat=ierror)
write(n_unit,*) (a(i), i = 1,10) ! write a(1) to a(10) to output.dat
...
...
close(unit=n_unit)
• OPEN (open_list)
commonly used clauses of open_list :
OPEN (unit=int_expr, file=char_expr1, status=char_expr2, &
& action=char_expr3, iostat=int_var)

• 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)

• The existing file input.dat is opened and assigned to logical unit 8.


• status='old' indicates the file already exits; if it does not, an error code is assigned to ierror
• action='read' the file should be read-only; if an attempt is made to write to the file, an error
code is assigned to ierror

Ex: integer :: ierror, n_unit=9


character(len=7) :: data_name='OUT.DAT'
open(unit=n_unit, file=data_name, status='NEW', &
& action='WRITE', iostat=ierror)
• A new file OUT.DAT is opened and assigned to logical unit 9.
• status='NEW' indicates the file is new; if it already exits, an error code is assigned to ierror.
• If status='REPLACE', a new file is open for output whether a file by the same name exits or not.
• action='WRITE' the file should be write-only

Ex: open(unit=12, status='SCRATCH', iostat=ierror)

• status='SCRATCH' means the file is a temporary file; it will be deleted automatically


once the file is closed or the program terminates.
• If the file is for scratch, no file name should be specified in OPEN statement.
• IOSTAT= clause in READ statement
READ(i, j, iostat=int_var)

。 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 .

Ex: ! read unknown number of data from a file


real :: a(1000), tmp
integer :: nvar=0, iopen, iread

open(unit=8, file='INPUT.DAT', status='OLD', iostat=iopen)


if(iopen == 0) then ! iopen=0 means open file successfully
do
read(8,*,iostat=iread) tmp ! iread=0 means read successfully
if(iread < 0) exit
nvar=nvar+1
a(nvar)=tmp
end do
end if
...
Example: read values from an input data set, sort the data into ascending order
PROGRAM sort1
!
! Read in a input data set, sort it into ascending order using the
! selection sort algorithm, and write the sorted data to the
! standard output device.
!
IMPLICIT NONE
INTEGER, PARAMETER :: MAX_SIZE = 1000 ! Maximum input data set size
REAL, DIMENSION(MAX_SIZE) :: a ! Data array to sort
CHARACTER(len=20) :: filename ! Input data file name
INTEGER :: i ! Loop index
INTEGER :: iptr ! Pointer to smallest value
INTEGER :: j ! Loop index
INTEGER :: nvals = 0 ! Number of data values to sort
INTEGER :: status ! I/O status: 0 for success
REAL :: temp ! Temporary variable for swapping

! Get the name of the file containing the input data.


WRITE (*,1000)
1000 FORMAT ('Enter the file name with the data to be sorted: ')
READ (*,'(A20)') filename

! Open input data file.


OPEN ( UNIT=9, FILE=filename, STATUS='OLD', ACTION='READ', &
& IOSTAT=status )
Continued on next page...
! Was the OPEN successful?
fileopen: IF ( status == 0 ) THEN ! Open successful
! Read the data
DO
READ (9, *, IOSTAT=status) temp
IF ( status /= 0 ) EXIT ! Exit on end of data file
nvals = nvals + 1 ! Number of data count
a(nvals) = temp ! Save value in array
END DO

! Sort the data


outer: DO i = 1, nvals-1
! Find the minimum value in a(i) through a(nvals)
iptr = i
inner: DO j = i+1, nvals
minval: IF ( a(j) < a(iptr) ) THEN
iptr = j
END IF minval
END DO inner

! iptr points to the minimum, swap a(iptr) with a(i) if i /= iptr


swap: IF ( i /= iptr ) THEN
temp = a(i)
a(i) = a(iptr)
a(iptr) = temp
END IF swap
END DO outer Continued on next page...
! Now write out the sorted data.
WRITE (*,'(1X,A)') 'The sorted output data values are: '
WRITE (*,'(4X,F10.4)') ( a(i), i = 1, nvals )

ELSE fileopen

! Else file open failed. Tell user.


WRITE (*,1050) status
1050 FORMAT (1X,'File open failed--status = ', I6)

END IF fileopen

END PROGRAM sort1


Exercise
Table of Logarithms : Write a Fortran program to generate a table of the base 10 logarithms
between 1.0 and 10.9 in steps of 0.1. Output the table to file log_tab.dat. The table
should be organized as follows:

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

2.0 0.301 0.322 0.342 0.362 ...

3.0 ...

4.0 ...

5.0 ...

6.0 ...

7.0 ...

8.0 ...

9.0 ...

10.0 ...

You might also like