0

In this program, I am attempting to read a name and their GPA from a file. The first name is Audrey, then a white space, and then 3.6. The second line is Oakley, then a white space, and 3.5.

int main()
{
    FILE * fPtr;
    FILE * fPtr2;

    char x[10] = { "Oakley " };
    double y;
    int z;

    fPtr = fopen("data.txt", "r");

    if (fPtr == NULL) // open failure
        puts("File open for read failed");
    else
    {
        while (scanf("%d", &z) != EOF)
        {
            fscanf(fPtr, "%s", x);
            fscanf(fPtr, "%lf", &y);
            fprintf(stdout, "Value read = %s\n", x);
            fprintf(stdout, "GPA = %lf \n", y);
        }
    }

    fclose(fPtr);
    system("pause");
    return 0;
}

So, I tried this once before and it worked. In that attempt, "x[10] = Audrey" and this was the first name in the list. It worked, and the fscanf gave me her GPA. The second time, I tried scanning for Oakley and I still get Audrey, but when I remove that line I get a really large negative number.

I used fscanf because it tokenizes around whitespace, so my theory is that if the cursor gets to the proper name then it will read the next number and that will be the GPA? Right? How do I get it to search for Oakley?

  • 1
    You need to fix some basic errors in your code first, such as that `fPtr2` is never initialized, then is passed to `fclose()`, and that you do not check the return value of various functions like `fscanf()`. – John Zwinck Apr 28 '16 at 03:29
  • `fscanf` does not "search" as you seem to think. It only reads the input from the current file position and parse what is read according to the format you specify. – kaylum Apr 28 '16 at 03:29
  • I think your classmate is doing the [same homework](http://stackoverflow.com/questions/36904718/reading-and-printing-from-a-file-but-stuck-in-loop-c) also on stackoverflow. – TheGreatContini Apr 28 '16 at 03:35
  • Okay, I removed everything involving fPtr2. That's for later. Right now I'm having trouble getting the file position to where I want it? How do I do that? – Jeremy Morgan Apr 28 '16 at 03:36
  • You can't skip straight to the required entry as you don't know where it is before hand. What you need to do is to read each line and then use `strcmp` to check whether the name is the one you want. If it isn't then continue with reading the input. If it is then you have found it and can print the result. – kaylum Apr 28 '16 at 03:57
  • Okay, I can see why that would be useful. How, then, would I read the next input for GPA? – Jeremy Morgan Apr 28 '16 at 04:01

1 Answers1

1

You need check scanf for any errors, which could happen because the input file does not match the format you specified. Try these changes:

char user[100];

while (scanf("%s", user) == 1) {

    while (fscanf(fPtr, "%s %lf", x, &y) == 2)
    {
        if (strcmp(user, x) == 0) {
            fprintf(stdout, "GPA for %s is %lf \n", user, y);
            break;
        }
    }
    rewind(fPtr);
}

Also, fPtr2 is is uninitialized in your code, remove the line fclose(fPtr2).

fluter
  • 13,238
  • 8
  • 62
  • 100
  • Whoa, this gives me way too much lol. I had a while statement that read the file until the end, but I'm looking for a way to seek out the specific name and then print the number next to it. – Jeremy Morgan Apr 28 '16 at 03:40
  • @JeremyMorgan what the purpose of using `z` in your original code? – fluter Apr 28 '16 at 03:43
  • My purpose was because I wanted to say "while the user chooses to run the program..." because we have to ask the user to enter a name and then put out the GPA. When I get it to seek out a specific x[10], I will then make x[10] a user-input string and the z will be used to check for continuation – Jeremy Morgan Apr 28 '16 at 03:47
  • This code discards the items that were read on the first line; and then performs reading without error-checking... surely that is not what is intended – M.M Apr 28 '16 at 03:53
  • @JeremyMorgan so you want to output only the GPA of the matched user, which is input interactively? – fluter Apr 28 '16 at 04:24
  • @JeremyMorgan just add a break when the name matches, see my example code – fluter Apr 28 '16 at 05:09