I wrote a program that reads in a file into an array of structures. I am supposed to check that the data is valid while reading it in. It should read in 7 elements into the structure and print an Invalid Data message if anything besides 7 elements are read in correctly. The program has a loop that should break when the end of the file is reached but for some reason it still loops one more time and therefore prints a false Invalid Data statement. I can't figure out the logic to change my loop so this will not happen. Crazy idea but can i just somehow delete that last Invalid Data statement somehow??? Or how can I change the loop?
Here is the code
Code:
//open file
fp = fopen(argv[1], "r");
if (fp == NULL)
{
printf("Error: Could not open file for read.\n\n");
return EXIT_FAILURE;
} //if
//now open and take care of fscanf with : between fields
while ((fgets(buffer, 1000, fp) != NULL) && i < 100)
{
result = sscanf(buffer, "%20[A-za-z ,]:%6[0-9]:%d:%d:%d:%d:%s", stuAry[i].name, stuAry[i].studentID, &stuAry[i].scoreOne, &stuAry[i].scoreTwo, &stuAry[i].scoreThree, &stuAry[i].scoreFour,
stuAry[i].grade);
if (result == EOF)
{
break;
} /* close if */
else if (result != 7)
{
printf("Invalid Record: %s", buffer);
} /* close second if */else if (result == 7)
{
sum = (stuAry[i].scoreOne + stuAry[i].scoreTwo + stuAry[i].scoreThree + stuAry[i].scoreFour);
stuAry[i].average = (sum / 4);
i++;
} //if
} //while
and when I run the program I get this
Code:
Invalid Record: Data, Bad:9876543:100:34:54:21:F
Invalid Record: Data, Missing:123456:100::99:50:C
Invalid Record:
How can I get rid of that last line?
Thanks