• No results found

Data Files 121 int main(void)

In document Engineering Problem Solving With C (Page 144-149)

Reading Data Files

Section 3.7 Data Files 121 int main(void)

{

/* Declare and initialize variables. */

int num_data_pts, k;

double time, motion, sum=0, max, min;

FILE *sensor;

/* Open file and read the number of data points. */

sensor = fopen(FILENAME,"r");

if (sensor == NULL)

printf("Error opening input file. \n");

else {

fscanf(sensor,"%d",&num_data_pts);

/* Read data and compute summary information. */

for (k=1; k<=num_data_pts; k++) {

fscanf(sensor,"%lf %lf",&time,&motion);

if (k == 1)

max = min = motion;

sum += motion;

if (motion > max) max = motion;

if (motion < min) min = motion;

}

/* Print summary information. */

printf("Number of sensor readings: %d \n", num_data_pts);

printf("Average reading: %.2f \n", sum/num_data_pts);

printf("Maximum reading: %.2f \n",max);

printf("Minimum reading: %.2f \n",min);

/* Close file and exit program. */

fclose(sensor);

}

/* Exit program. */

return 0;

}

/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/

The following report will be printed by this program using the sensor1.txtfile:

Number of sensor readings: 10 Average reading: 149.77 Maximum reading: 169.30 Minimum reading: 132.50

Trailer or Sentinel Signals. Assume that the data file sensor2.txtcontains the same information as the sensor1.txtfile, but instead of giving the number of valid data records at the beginning of the file, a final record contains a trailer signal. The time value on the last line in the file will contain a negative value so that we know that it is not a valid line of infor-mation. A second number must be included on the trailer line, since the statement that reads each line expects two values; otherwise, an error occurs. The contents of the data file named sensor2.txtare as follows:

0.0 132.5 0.1 147.2 0.2 148.3 0.3 157.3 0.4 163.2 0.5 158.2 0.6 169.3 0.7 148.2 0.8 137.6 0.9 135.9 -99 -99

The process of reading and accumulating information until we read the trailer signal is easi-ly described using a do/whileloop structure, as shown in the following pseudocode and program:

Refinement in Pseudocode main: set sum to zero

set number of points to 0 if file cannot be opened

print error message else

read time, motion set max to motion set min to motion do

add motion to sum if motion > max

set max to motion if motion < min

set min to motion

increment number of points by 1 read time, motion

while time ≥ 0

set average to sum/number of data points print average, max, min

Section 3.7 Data Files 123

/* Declare and initialize variables. */

int num_data_pts=0;

double time, motion, sum=0, max, min;

FILE *sensor;

/* Open file and read the first data point. */

sensor = fopen(FILENAME,"r");

if (sensor == NULL)

printf("Error opening input file. \n");

else {

fscanf(sensor,"%lf %lf",&time,&motion);

/* Initialize variables using first data point. */

max = min = motion;

/* Update summary data until trailer record read. */

do

/* Print summary information. */

printf("Number of sensor readings: %d \n", num_data_pts);

The report printed by this program using the sensor2.txtfile is exactly the same as the re-port printed using the sensor1.txtfile.

End-of-File. A special end-of-file indicator is inserted at the end of every data file; the feof function in the Standard C library can be used to detect when this indicator has been reached in a data file. The fscanffunction can also be used to detect when the end of the data has been reached in a file. Recall that the fscanffunction returns the number of values successfully read each time that it is executed. Thus, if the function returns a value that is dif-ferent from the number of values that it was supposed to read, then the end of the data file has been reached, or there are errors in the information in the data file. If the information in the data file is valid, then the fscanffunction can be used to determine when the end of the data file is reached. Consider the following statements:

while ((fscanf(data,"%lf",&x)) == 1) {

count++;

sum += x;

}

ave = sum/count;

Thefscanffunction attempts to read a value for xfrom a data file. If a value is read, the function returns a value of 1, and the statements within the loop are executed. If the end of the data file is reached, there is no more data; thus, the function does not return a value of 1, and control passes to the statement following the whileloop.

We now assume that the data file sensor3.txtcontains the same information as the sensor2.txtfile, except it does not include the trailer signal. In the following pseudocode and program, we read and accumulate information until we reach the end of the data file:

Refinement in Pseudocode main: set sum to zero

set number of points to 0 if file cannot be opened

print error message else

while not at the end of the file read time, motion

add 1 to the number of points if k=1

set max to motion set min to motion add motion to sum if motion > max

set max to motion if motion < min

set min to motion

set average to sum/number of data points print average, max, min

End-of-file indicator

Section 3.7 Data Files 125

/* Declare and initialize variables. */

int num_data_pts=0;

double time, motion, sum=0, max, min;

FILE *sensor;

/* Open file. */

sensor = fopen(FILENAME,"r");

if (sensor == NULL)

printf("Error opening input file. \n");

else {

/* While not at the end of the file, */

/* read and accumulate information. */

while ((fscanf(sensor,"%lf %lf",&time,&motion)) == 2) {

num_data_pts++;

/* Initialize variables using first data point. */

if (num_data_pts == 1)

/* Print summary information. */

printf("Number of sensor readings: %d \n", num_data_pts);

In two of the programs developed in this chapter, the loop contained a condition that tested for the first time that the loop was executed. When the condition was true, the maxandmin values were initialized to the first motionvalue. If the data files used with these programs were long, the time required to execute this selection statement could be substantial. One way to avoid this test is to read the first set of data and initialize the variables before entering the loop. This change may also require other changes in the program.

1. Modify program chapter3_5to remove the condition that tests for the first time that the loop is executed.

2. Modify program chapter3_7to remove the condition that tests for the first time that the loop is executed.

M O D I F Y ! M O D I F Y !

The report printed using the sensor3.txtfile is exactly the same as the report printed using sensor1.datorsensor2.txt.

All three file structures are commonly used in engineering and scientific applications.

Therefore, it is important to know which type of structure is used when you work with a data file. If you make the wrong assumption, you may get incorrect answers instead of an error message. Sometimes the only way to be sure of the file structure is to print the first few lines and the last few lines of the file.

In document Engineering Problem Solving With C (Page 144-149)