ICS 101 - Computer programming
Lab # 13
File Processing
Objectives
Read from
File
Write to
File
Example
Write a program that reads unknown number of integers from a file INPUT.DAT and writes the positive integers into a file POSITIVE.DAT and the negative integers into a file NEGATIVE.DAT. Assume that each line of the file INPUT.DAT contains one value.
Solution
1. Create New Project workspace (e.g. LAB13)
2. Add data file with a name (e.g. INPUT.DAT) in the project workspace
3. In the active window, type in the following input
4. Add FORTRAN file with a name (e.g. P1.FOR) in the same project workspace
5. In the active window, type in the following program starting after the green line.
Following is the output of the program
6. After the program is executed, it will create the following two output files:
Exercise 1
Write a program that reads unknown number of positive integers from a file INPUT.DAT and writes the odd integers into a file ODD.DAT and the even integers into a file EVEN.DAT. Assume that each line of the file INPUT.DAT contains one value.
Exercise 2
Each line of a file INPUT.DAT contains the ID number of a student and his three quiz grades (Real numbers). The number of data lines in the file is not known. Write a program which will read the data from the file and print in another file OUTPUT.DAT each student ID and his average in the three quizzes. The program should then print in the OUTPUT.DAT file the overall average.
Your input:
1001 51.0 67.0 80.0
1002 90.0 56.0 76.0
1003 68.0 80.0 92.0
1004 85.0 31.0 67.0
Your output:
STUDENT ID QUIZ AVERAGE
1001 66.000000
1002 74.000000
1003 80.000000
1004 61.000000
the overall average: 70.250000
Exercise 3
Write a program that reads unknown number of reals from a file and prints the percentage of values above average. Do not use an array. Assume that each line of the file contains one value. HINT: Use the REWIND statement.