0

Hi I am trying to create a function which will take input from the user in the form of a string search a binary file which contains football results e.g (Chelsea,liverpool,1,2) and print each time the string appears for example if the user enters liverpool then all of liverpool's results will be printed to the screen.The code I have does not print anything. What am I doing wrong? Here is what I have.

struct matches 
{
    //structure members
    char teamA [100]; 
    char teamB [100];
    int goalsA;
    int goalsB;
};    
void displayMatches()
    {   
        struct matches match; //declare variable of type struct

        char input[20]; //variable to take input from user 

        FILE * file1; //pointer to a file

        file1 = fopen("matches.bin","rb");//open binary file in read mode 

        if(file1==NULL) //error check for file
        {
            printf("open file error.\n");
            exit(6);
        }

        printf("Enter the team\'s results you want to view\n");
        scanf("%s",input);

        /*loop to read binary file check if input team 
         is in file if yes print the results for that team */
        while(fread(&match,sizeof(match),1,file1)) 
        {



            if(strstr(match.teamA,input))
            {

                printf(" %s %s %i %i ",match.teamA,match.teamB,match.goalsA,match.goalsB);

            } 
            else if(strstr(match.teamB,input))
            {

                printf(" %s %s %i %i ",match.teamA,match.teamB,match.goalsA,match.goalsB);

            }

        }

        fclose(file1);
    }
user3438340
  • 49
  • 1
  • 4
  • 1
    Note that you process the last unit of input twice: once when you read it, and a second time after `fread()` reports that it didn't read anything but you ignore it (so the loop processes the last unit of data a second time). See [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler May 04 '14 at 21:36
  • You should output a newline after each reported match. You might have problems because of the structure of your `struct match`; we can't see it so it isn't clear. You'd have problems if it contains pointers, but you probably won't have problems if it contains arrays of characters for the team names, etc. Since you use the same (modestly) complicated `printf()` for both types of match, you should probably combine the conditions into `if (strstr(…) || strstr(…))` — DRY: Don't repeat yourself. – Jonathan Leffler May 04 '14 at 21:41
  • As a general debugging technique, print the data as you read it, so you know that the program is seeing the information you think it is seeing. So, echo what the user entered; also show what's in each match as it is read, so you know whether the `fread()` is getting what you expect it to get. – Jonathan Leffler May 04 '14 at 21:43

1 Answers1

0

Change from

while(feof(file1) == 0)

to

while(feof(file1) != 0)

That might be the issue.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
he_wolf
  • 21
  • 4
  • 1
    But see [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler May 04 '14 at 21:35
  • Remember that `feof()` returns a non-zero value when EOF is reached, so it returns a zero value until EOF _is_ returned. The standard idiom is `while (!feof(file1))`, but the idiom itself is flawed. It can be used, redundantly, if you also check the return value from the actual input operations: `if (fread(…) == 1) { …OK to use data… }`. Better would be `while (fread(…) == 1) { …process the data… }`. Your suggestion, however, inverts the condition incorrectly. – Jonathan Leffler May 04 '14 at 21:47
  • Hi he_wolf thank you for your response tried that and still nothing is getting printed – user3438340 May 04 '14 at 21:52