Reading on Text Files
Introduction
Many applications involve analysis of data found in a file (e.g., a
spreadsheet or text-based documenbt). As an example, this reading outlines
several approaches to the following problem.
Problem
File class-tests.dat shows test scores for a
hypothertical class. The file is organized as follows:
-
The first four lines contain title information and column headers.
-
Data for each class member is on its own line.
-
The name appears in columns 1-20
-
Test 1 is located in columns 21-30.
-
Test 2 is located in columns 31-40.
-
Test 3 is located in columns 41-end.
-
No line of data contains more than 50 characters.
A program is to be created to print scores for a designated test or the average
test score for each person, according to a command-line parameter:
-
print name and scores for test 1 if command line parameter is 1
-
print name and scores for test 2 if command line parameter is 2
-
print name and scores for test 3 if command line parameter is 3
-
print name and average on the three tests if command line parameter is all
Approach 1: Read name into array and read scores with fprintf
class-tests-1.c
Approach 2: Read full line at a time
class-tests-2.c