6.5 Processing Records
Sometimes, the text file may contain records of information in which each record contains multiple fields of information. For example, suppose the data file contained both the id number and the exam grade for each student in the following format:
245 85 189 90 175 89 206 75 122 100 284 98 111 86
Since each record consists of two fields, only the first field has to be read before testing for the end of file. Here we would read the student id number as the priming read, then test for the end of file. If the end of file was not reached, we then read the rest of the record before processing the data. These steps are illustrated in the following code segment:
- int grade, id;
- int sum = 0;
- int count = 0;
- fscanf(infile, "%d", &id); // priming read - read first record field (id num)
- while(!eof(infile)) {
- fscanf(infile, "%d", &grade); // read the rest of the record
- sum = sum + grade;
- count = count + 1;
- fscanf(infile, "%d", &id); // modification read
- }
Remember, the modification read should always be the same operation as the priming read. Thus, we only read the id number at the bottom of the loop.
Worked Example
Now consider a more detailed problem in which the text file contains three exam grades for each student in the following format:
245 85 70 90 189 90 87 92 175 89 98 92 206 75 70 81 122 100 98 99 284 98 90 86 111 86 65 71
The objective is to compute the average exam grade for each individual student and produce a report similar to the following:
Id # Avg Grade ---------------- 245 81.67 189 89.67 175 93.00 206 75.33 122 99.00 284 91.33 111 74.00
In solving this problem, we must compute the average grade for each student based on the scores for three exams. In the previous example, there was a single grade per student, which could be read as the first statement within the while loop.
After reading the first id number and testing to make sure we have not reached the end of file, we will need to read the three exam grades using a loop. Here we use a for loop (and assume sum was previously declared as an integer):
- sum = 0;
- for(int i = 0; i < 3; i++) {
- fscanf(infile, "%d", &grade);
- sum = sum + grade;
- }
Once all of the grades have been read and summed, we can now compute the average grade for the student (where avg is a floating-point variable):
- avg = sum / 3.0;
To complete the processing of the record, next we print the results for the current student:
- printf("%4d %9.2f\n", id, avgGrade);
After printing the results, we need to read the next id number as the modification read. This process continues until all of the records have been read and processed. In a well-designed program, these steps would be placed within a separate function that can be called after opening and verifying the file.
A complete solution for this problem is provided in the program listing below. Take time to review the source code as it also illustrates a top-down design and the use of functions in such a design.
Program: procgrades.cc
|