0% found this document useful (0 votes)
33 views

Computer Programming in Fortran 77: Lecture 8 - Output Design and File Processing

The document discusses file processing in Fortran 77, including opening files for input and output, reading from and writing to files, working with multiple files, and closing files. It provides examples of opening files, reading data from files into variables, writing output to files, reading unknown numbers of values from files, and concatenating the contents of multiple files into a single output file.

Uploaded by

armanimams
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Computer Programming in Fortran 77: Lecture 8 - Output Design and File Processing

The document discusses file processing in Fortran 77, including opening files for input and output, reading from and writing to files, working with multiple files, and closing files. It provides examples of opening files, reading data from files into variables, writing output to files, reading unknown numbers of values from files, and concatenating the contents of multiple files into a single output file.

Uploaded by

armanimams
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Computer Programming

in Fortran 77

Lecture 8 Output Design and File Processing


File Processing
Why Files?
Huge data
Repetitive data is needed for more than one program
Data may be entered by other people or instruments

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:

OPEN(UNIT = 1, FILE = 'POINTS.DAT', STATUS = 'OLD')

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:

OPEN(UNIT = 1, FILE = 'RESULT.DAT', STATUS = 'UNKNOWN')


Reading from Files
READ(UNIT, *) VARIABLE LIST

Example 1: Find the sum of three exam grades taken from file EXAM.DAT.

Solution:

INTEGER EXAM1, EXAM2, EXAM3, SUM


OPEN(UNIT = 10, FILE = 'EXAM.DAT', STATUS = 'OLD')
READ(10, *) EXAM1, EXAM2, EXAM3
SUM = EXAM1 + EXAM2 + EXAM3
PRINT*, SUM
END
Reading from Files (With
Unknown Length)
READ(UNIT, *, END = NUMBER) VARIABLE LIST

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:

REAL NUM, SUM, AVG


INTEGER COUNT
OPEN(UNIT = 12, FILE = 'NUMS.DAT', STATUS = 'OLD')
SUM = 0.0
COUNT = 0
333 READ(12, *, END = 999) NUM
SUM = SUM + NUM
COUNT = COUNT + 1
GOTO 333
999 AVG = SUM / COUNT
PRINT*, 'AVERAGE =', AVG
END
Writing to Files
WRITE(UNIT, *) EXPRESSION LIST

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

Assume that the file 'INPUT.DAT' contains the following:

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

Assume that the file 'INPUT.DAT' contains the following :

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

Assume that the file 'INPUT.DAT' contains the following data

4 5
6 7
8

You might also like