Again we will read the input values from a text file

Info icon This preview shows pages 12–18. Sign up to view the full content.

View Full Document Right Arrow Icon
again We will read the input values from a text file, as before We will continue reading axle/weight pairs until we reach the end of the file We will need to redesign two functions we wrote in class a couple of weeks ago: - read_num_axles - read_weight Let's look at the implementation…
Image of page 12

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
13 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Endfile-Controlled Loops (cont.) fscanf actually returns a value indicating the number of items it successfully reads in If it encounters the end of the file, it returns as its result the value of the standard constant EOF (which is a negative integer) We can thus redesign read_num_axles to return EOF if it encounters the end of the file: int read_num_axles(FILE *infile) { int num_axles, input_status; input_status = fscanf(infile,”%d”,&num_axles); if (input_status != EOF) return (num_axles); else return input_status; }
Image of page 13
14 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Endfile-Controlled Loops (cont.) For the sake of simplicity, we will assume that data always come in pairs, so we won't check for EOF in read_weight Challenge question: Do you see another problem with adding EOF-checking to read_weight? Explain. The next slide puts the solution together
Image of page 14

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
15 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Endfile-Controlled Loops (cont.) #include <stdio.h> #define POUNDS_PER_TON 2000 void main() { int count, test_axles, axles; double weight, tons, toll, total_tolls; FILE *infile; infile = get_and_open_file(); count = tons = total_tolls = 0; test_axles = read_num_axles(infile); while (test_axles != EOF) { axles = test_axles; /* We can accept this value as valid. */ weight = read_weight(infile); /* Assume EOF won't be seen. */ toll = compute_toll(axles,weight); count++; /* Another car has crossed. */ total_tolls += toll; /* Another toll has been collected. */ tons += weight/2000.0; /* Add weight of this car to total*/ test_axles = read_num_axles(infile); /* Prepare for next */ /* iteration. */ } }
Image of page 15
16 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Flag-Controlled Loops In the previous examples, we have assumed that input data are always in the proper format: When we ask for the number of axles, we will obtain an integer (either interactively from the user, or from the input file) When we ask for the weight, we will obtain a double (either interactively from the user, or from the input file) In the real world, this assumption is faulty People enter invalid data all the time Files contain invalid data all the time Flag-controlled loops ensure that valid data are read in
Image of page 16

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
17 C ptS  121 L6-3 – 10/2/09 Pro f. C hris  Hundhaus e n Flag-Controlled Loops (cont.) Recall that the fscanf function returns EOF when the end of the file is encountered Likewise fscanf and scanf return the value 0 when at least one the data values it reads in could not be converted to the specified type For example, assume the following scanf statement int my_int, input_status; printf("Please enter an integer: "); input_status = scanf("%d",&my_int); If the user were to type in "wow" here, i nput_status would be assigned the value 0 , since "wow" cannot be converted to an int
Image of page 17
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern