CMG
CMG

Reputation: 53

fscanf won't read the integer values in text file

I am using an fscanf to read a sequence of 8 values of zero or one at the end of the line, but it looks like the array is never having a value read to it.

It is reading this from the text file:

Chilli Con Carne,Chilli_Con_Carne.txt, 15, 25, 100,0,0,0,0,0,0,0,0
Spaghetti Bolognese,Spaghetti_Bolognese.txt, 30, 75, 150, 0, 0, 0, 0, 0, 0, 1, 0
Frittata,Frittata.txt, 10, 15, 160, 1, 1, 0, 0, 0, 0, 0, 0
Korean Beef,Korean_Beef.txt, 20, 30, 100, 0, 0, 0, 0, 0, 1, 0, 1
Trawlers Pie, Trawlers_Pie.txt, 20, 30, 100,1,1,0,1,0,0,0,0

This is the loop I am using which contains the fscanf that is not reading the recipeAllergies:

while (!feof (cookBook)){

    fscanf(cookBook, "%20[^,], %20[^,], %d, %d, %d", recipeName, recipeFileName, &prepTime, &cookTime, &calories);
        printf("%s ", recipeName);

        for (i = 0; i < 8; i++){

            fscanf(cookBook, "%d,", &recipeAllergies[i]);
                printf("%d %d\n", recipeAllergies[i], userAllergies[i]);

            if ((userAllergies[i] + recipeAllergies[i]) == 2){
                notSuitable = 1;
            }

        }
        fgets(ignore_line, sizeof(ignore_line), cookBook);

        if (notSuitable == 0){
            (suitableRecipes[totalSuitable]. recipeName, recipeFileName, prepTime, cookTime, calories);
             totalSuitable++;
         }
}

Any help is greatly appreciated.

Upvotes: 1

Views: 2315

Answers (2)

Peter
Peter

Reputation: 36637

The inner loop has a format string "%d," so fscanf() is looking for integers followed by a comma. There is no comma on the end of the input lines, so scanf() will return indicating an error.

Either add commas at the end of the input lines, or change the loop so it doesn't look for a comma the 8th time. The latter is preferable.

Although you didn't ask, a few other concerns you might want to address with your code......

  • Looping on feof() is a really bad idea, because (among other problems) if no error is detected in the loop body, it will attempt to read one more line of input from your file than exists.
  • Always check the return value of functions like fscanf() and fprintf(), since they can fail.
  • Mixing fgets() with fscanf() on the same file is a bad idea, because they respond to file contents differently (e.g. in how they respond to a newline) and using them in combination can result in spurious loss of data. [More generally, the same goes for mixing styles of input. Don't mix manners of input for the same file. Mixing character oriented input (e.g. fgetc()), line oriented input (e.g. fgets()), and/or formatted input (e.g. fscanf()) can all cause spurious problems if they are used on the same stream.]
  • The first statement in the if (notSuitable == 0) block doesn't do anything. I assume you left out the name of the function to be called.

Upvotes: 1

doptimusprime
doptimusprime

Reputation: 9411

First, process comma after calorie while using fgetc. After that, read each number.

You are also expecting comma from last digit where as this might not be the case.

Alternatively, you can discard each comma separately by using fgetc.

You can try following code snippets in your for loop.

   for (i = 0; i < 8; i++){
        fgetc(cookBook); //To discard/read comma
        fscanf(cookBook, "%d", &recipeAllergies[i]);
            printf("%d %d\n", recipeAllergies[i], userAllergies[i]);

        if ((userAllergies[i] + recipeAllergies[i]) == 2){
            notSuitable = 1;
        }

    }

Also see the link mentioned in question's comment. Why is “while ( !feof (file) )” always wrong?

Upvotes: 0

Related Questions