Computer Programming in Fortran 77: Lecture 8 - Output Design and File Processing
Computer Programming in Fortran 77: Lecture 8 - Output Design and File Processing
in Fortran 77
Opening Files
Preparing a file for input:
OPEN(UNIT = INTEGER EXPR, FILE = 'FILENAME', STATUS
= 'OLD')
Preparing a file for output:
OPEN(UNIT = INTEGER EXPR, FILE = 'FILENAME', STATUS
= 'NEW')
Or OPEN(UNIT = INTEGER EXPR, FILE = 'FILENAME', STATUS
= 'UNKNOWN')
Examples
Example 1: Assume that you want to use file POINTS.DAT as an input file. The
following statement will then appear before any read statement from the file:
Example 2: Assume that you want to use file RESULT.DAT as an output file.
The following statement will then appear before any write statement to the
file:
Example 1: Find the sum of three exam grades taken from file EXAM.DAT.
Solution:
Example 2: Find the average of real numbers that are stored in file NUMS.DAT. Assume
that we do not know how many values are in the file and that every value is stored on a
separate line.
Solution:
Example: Create an output file CUBES.DAT that contains the table of the cubes
of integers from 1 to 20 inclusive.
Solution:
INTEGER NUM
OPEN(UNIT = 20, FILE = 'CUBES.DAT', STATUS = 'UNKNOWN')
DO 22 NUM = 1, 20
WRITE(20, *) NUM, NUM**3
22 CONTINUE
END
Working with Multiple Files
Example: Create an output file THIRD that contains the values in file FIRST followed by
the values in file SECOND. Assume that every line contains one integer number and we do
not know how many values are stored in files FIRST and SECOND.
Solution:
INTEGER NUM
OPEN(UNIT = 15, FILE = 'FIRST.DAT', STATUS = 'OLD')
OPEN(UNIT = 17, FILE = 'SECOND.DAT', STATUS = 'OLD')
OPEN(UNIT = 19, FILE = 'THIRD.DAT', STATUS = 'UNKNOWN')
33 READ(15, *, END = 66) NUM
WRITE(19, *) NUM
GOTO 33
66 READ(17, *, END = 30) NUM
WRITE(19, *) NUM
GOTO 66
30 STOP
END
Closing/Rewinding
Closing Files
CLOSE (UNIT)
Rewinding Files
REWIND (UNIT)
Example:
Given a data file INPUT.DAT that contains unknown number of lines. Each line has
a student ID and his grade. Write a program that reads the data from the above
file and writes to a new file OUTPUT.DAT in each line the ID and the grade of all
the students who have grades greater than the average.
REAL GRADE , SUM, AVG
INTEGER ID, COUNT , K
OPEN (UNIT = 20, FILE = INPUT.DAT, STATUS = OLD)
OPEN (UNIT = 21, FILE = OUTPUT.DAT, STATUS =
UNKNOWN)
SUM = 0.0
COUNT = 0
44 READ (20, * , END =100 ) ID, GRADE
SUM = SUM + GRADE
COUNT= COUNT+ 1
GOTO 44
100 AVG = SUM/ COUNT
REWIND (20)
DO 50 K = 1 , COUNT
READ (20, * ) ID, GRADE
IF (GRADE. GT. AVG) WRITE (21, *) ID, GRADE
50 CONTINUE
END
Exercise
What will be printed by the following program?
INTEGER M, K
OPEN ( UNIT = 10, FILE = 'INPUT.DAT', STATUS = 'OLD')
READ ( 10, *, END = 10) ( M, K = 1, 100)
10 PRINT*, M, K - 1
END
1 2 3
4 5 Output
6 7 8 9
6 6 10
What will be printed by the following program?
INTEGER M
OPEN ( UNIT = 10, FILE = 'INPUT.DAT',STATUS = 'OLD')
READ (10,*) M
20 IF ( M .NE. -1) THEN
PRINT*, M Output
READ(10, *, END = 30) M 7
GOTO 20 3
ENDIF 9
PRINT*, 'DONE' 4
DONE
30 PRINT*, 'FINISHED' FINISHED
END
7
3
9
4
-1
What will be printed by the following program?
INTEGER A, B
OPEN ( UNIT = 10, FILE = 'INPUT.DAT', STATUS = 'OLD')
OPEN ( UNIT = 11, FILE = 'OUTPUT.DAT', STATUS = 'NEW')
READ*,A, B
READ(10, *) A, B, A
WRITE(11,*) A, B
READ(10, *, END = 10) A, B
10 WRITE(11, *) A, B Output
END
6 5
8 5
Assume the input for the program is:
10 11
4 5
6 7
8