Stopping fscanf?

2019-09-17 09:50发布

问题:

I'm writing a grade book program for school and I'm confused about one thing. We have a file to read from that has multiple student IDs and multiple scores. Would doing this:

results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);

store the number into those variables as it reads then stop the cursor when it runs out of variables to store the information in?...then should I jump right into a calculation function to calculate the final grade before having it loop the fscanf for the next student?

results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);
          getCalc(sco1, sco2, sco3);

Is that allowed? Thank you for your help.

回答1:

The following is a working example giving the average grade for every student id:

#include <stdio.h>

int main(void)
{
        int     id, sco1, sco2, sco3;

        while (fscanf(stdin, "%d%d%d%d", &id, &sco1, &sco2, &sco3) == 4)
        {
                printf("%d: %g\n", id, (sco1+sco2+(double)sco3)/3.0);
        }
        return 0;
}


回答2:

Would doing this:

results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);

store the number into those variables as it reads then stop the cursor when it runs out of variables to store the information in?

No, unless it is in a while loop, it stops exactly after reading the four entries ONCE, unless you are using the value from results to control a while loop and call fscanf() multiple times to scan 4 entries at every call.

Example:-

//Keeps reading unless it encounters less than 4 parameters, which might be the case
//if an end of file is reached, or your data pattern changes.

while(  fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3) == 4 )
{
 //You can pass the data of individual id, to calculate function, and compute the required
 //sum, total or other data locally in the function, there is really no reason to use pass
 //address in your case, so just transfer data using pass by value method. 
 getCalc(id,sco1,sco2,sco3);   
}


回答3:

In response to your first and second questions: The type of id, sco1, sco2 and sco3 should be int * (that is, a pointer to an int), these variables should explicitly point to actual int objects and you should check the return value (results) before you use them. For example:

int a, b, c, d;
int *id = &a, *sco1 = &b, *sco2 = &c, *sco3 = &d;
int results = fscanf(gradebook, "%d %d %d %d", id, sco1, sco2, sco3);
if (results == 4) {
    getCalc(sco1, sco2, sco3);
}

Furthermore, getCalc should accept arguments of the type int *. With all of these requirements fulfilled, the answer to your third question is: Yes, you can call getCalc with sco1, sco2 and sco3 as arguments.

As you have probably guessed, the intermediate pointer variables are unnecessary here, and this can be simplified. However, this simplification requires a modification to your fscanf expression (the insertion of &addressof operators):

int id, sco1, sco2, sco3;
int results = fscanf(gradebook, "%d %d %d %d", &id, &sco1, &sco2, &sco3);
if (results == 4) {
    getCalc(&sco1, &sco2, &sco3);
}

Which book are you reading?



标签: c fopen scanf