I have to create function that reads the binary file of futball matches and shows the legue table. The table should contains, for each team, the name of the team, number of points, goals scored and goals conceded. Give 3 points for a win and 1 for a draw.

The format of the binary file is:
Inter Milan,Real Madrid,2,1
Real Madrid,Juventus,3,1
Juventus,Barcelona,1,0
Barcelona,Chelsea,0,5
Chelsea,Manchester Utd,5,2
...

I got the next code, but it comparing only first column of strings and ignoring second column. I can't find the error.
Can you help me please.

/*Implement  league_table()*/
void league_table(FILE *binary,struct match single_match,team_no team_array[])
{
    struct match buffer[SIZE];
    int read = 0;
    int count=0;
    int i,j;
    int check;
    int numA,numB;
    int scored[10]={0};
    int conc[10] = {0};
    int points[10] = {0};
    printf("\n   League Table   \n");
    printf("________________________________________________________________________________\n");
    printf("--------------------------------------------------------------------------------\n");

    i = 0;
    //read the binary file and copy teams name to the team struct
    if((binary = fopen("matches.bin","rb"))!= NULL)
    {
        while(feof(binary) == 0 && i<180)
        {
            read = fread(&single_match,sizeof(struct match),1,binary);
            if(read == 0)
            {
                printf("\n Error in reading matches.bin");
            }
            else
            {
                //printf(" %s %s %d %d",single_match.team_A, single_match.team_B, single_match.goals_A, single_match.goals_B);

                strcpy(buffer[i].team_A, single_match.team_A);
                strcpy(buffer[i].team_B, single_match.team_B);
                buffer[i].goals_A = single_match. goals_A;
                buffer[i].goals_B = single_match. goals_B;
                i++;
            } 
        }
    }     
    else
    {
        printf("\n Error in opening binary file to read!!!");
        exit(1);
    }  
    fclose(binary);

    i = 0;

     //copy teams names to the struct team
    for(i = 0; i<10; i++)
    {
        strcpy(team_array[i].name,buffer[i].team_A);
        team_array[i].number = i + 1;
       // printf(" %s %d ",team_array[i].name, team_array[i].number);
    }

     // count points ,scored goals and conceded goals
    for(i = 0; i < 10; i++)
    {
        for(j = 0; j<SIZE; j++)
        {
            check = strcmp(team_array[i].name,buffer[j].team_A);
            if(check == 0)
            {
                numA = buffer[j].goals_A;
                scored[i] = scored[i] + numA;
                numB = buffer[j].goals_B; 
                conc[i] =  conc[i] + numB;
                count++;        
                if(buffer[j].goals_A > buffer[j].goals_B)
                {
                    points[i] = points[i] + 3;
                }
                if(buffer[j].goals_A == buffer[j].goals_B)
                {
                    points[i] = points[i] + 1;
                }
                else
                {
                   points[i] = points[i] + 0; 
                }
                //printf("\n %s   %s  %d  %d ",buffer[j].team_A, buffer[j].team_B, buffer[j].goals_A, buffer[j].goals_B);
            }  
            else
            {
                check = strcmp(team_array[i].name,buffer[j].team_B);
                if(check == 0)
                {
                    numB = buffer[j].goals_B;
                    scored[i] = scored[i] + numB;
                    numA = buffer[j].goals_A; 
                    conc[i] =  conc[i] + numA;
                    count++;

                    if(buffer[j].goals_A < buffer[j].goals_B)
                    {
                       points[i] = points[i] + 3;
                    }
                    if(buffer[j].goals_A == buffer[j].goals_B)
                    {
                        points[i] = points[i] + 1;
                    }
                     printf("\n %s   %s  %d  %d ",buffer[j].team_A, buffer[j].team_B, buffer[j].goals_A, buffer[j].goals_B);
                } 
            }                
        }
        printf("   %d |", count);
        printf("   %d", scored[i]);
        printf("   %d", conc[i]);
        printf("   %d\n\n",points[i]);
        count = 0;   
    }  

    i = 0;
    for(i = 0; i<10; i++)
    {
        team_array[i].scored = scored[i];
        team_array[i].conceded = conc[i];
        team_array[i].points = points[i];
        //printf("  %d",team_array[i].number);
        printf("%s ",team_array[i].name);
        printf("\t%i  ",team_array[i].scored);  
        printf("\t%i  ",team_array[i].conceded);
        printf("\t%i  \n",team_array[i].points);
    }

}//end league_table()

If the sample of the file you presented is accurate, then it's not a binary file, it's a variant of CSV. fread with that structure won't work since the file has to be parsed as a comma separated format. For it to work, the file would need to be created with fwrite using an object of the structure, and opening the file certainly won't give you a comma separated format.

So an underlying problem is that your file format does not match your processing code.

For clarification, the only difference between a 'binary' file and a 'text' file in C is that a text file stream will convert the platform's newline sequence into '\n'. You can use fread/fwrite on a text stream and fscanf/fprintf or any of the texty functions on a binary stream. I think one confusion point of C is that binary-oriented streams are somehow special, which they're not.

Thank you for help
But those pies of file is text file that should be converted to the binary file.
i have a separate function that convert .txt to .bin. Maybe my convert function is incorrect?

/* Implement convert() */
int convert(FILE *text,FILE *binary, struct match single_match)
{
    int count = 0;
    int check;

    // Open the source file for reading
    if((text=fopen("matches.txt","r"))==NULL)
    {
        printf("\n open read file error.\n");
        exit(1);
    }
    // Open the destination file for writing
    if((binary=fopen("matches.bin","wb"))==NULL)
    {
        printf("\n open write file error.\n");
        exit(1);
    }
     // while not end of file for the source file
    while(fscanf(text, "%[^,],%[^,],%d,%d",single_match.team_A, single_match.team_B, &single_match.goals_A, &single_match.goals_B)!= EOF)
    {    
        count = fwrite(&single_match,sizeof(single_match),1,binary);   //write 1 structure
        if(count == 0)
        {               //check if any error writing
            printf("\n write file error.\n");
            check = 0;
            exit(1);
        }
        else
        {
            check = 1;
        }
    }   

    fclose(text);
    fclose(binary);
    return(check);
}//end convert()

Let's have a look on my whole program maybe you will spot the problem.
Please help me to find solution

/* Date: 09/03/2015
Author: Nataliya Kizyuk
Assignment for Programming with Persistent data. Due to 07/05/2015
Program to: 1) Convert the text file matches.txt into a binary file matches.bin, 
                    2) Create a function that reads the binary files of matches and shows the league table
                    3) Create a function to insert a new results
                    4) Create a function to display the match with the highest number of goal scored. 
                    5) Create a function that displays all the matches of a team
                    6) Implement a text menu
*/
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
#define SIZE 180

//structure template
struct match
{
    char team_A[16];
    char team_B[16];
    int goals_A;
    int goals_B;
};

 struct team
{
    int number;
    char name[16];
    int points;
    int scored;
    int conceded;
};
typedef struct team team_no;

//Prototypes
int convert(FILE*,FILE*,struct match);
void league_table(FILE*,struct match,team_no team_array[]);
void new_results(FILE*,struct match);
void display_highest(FILE*,struct match);
void dislay_all(FILE*,struct match);
void exit(void);

main()
{
  FILE *text;
  FILE *binary;
  struct match single_match;
  team_no team_array[10];//the database
  int count1 = 0;     //declaring counter for executing enter code()
  int count2 = 0;     //declaring counter for executing encrypt_code()
  int count3 = 0;     //declaring counter for executing decrypt_code()
  char option = 0;    //declaring char variable to store inputed option
  int num; 
  int converted = 0;

  printf(" \n                WELCOME TO THE PROGRAM MENU \n");

  do // Start of 'do...while' that make program to run continually and re - display the menu while option 4 will be entered
  {

        printf("\n");   //BORDERS FOR MENU SCREEN
        printf("        _____________________________________________________________\n" ); 
        printf("       |                                                             |\n");
        printf("       |     MENU                                                    |\n");
        printf("       |                                                             |\n");
        printf("       |  1.Convert file.txt to file.bin                             |\n");
        printf("       |                                                             |\n");
        printf("       |  2.Shows the league table                                   |\n");
        printf("       |                                                             |\n");
        printf("       |  3.Insert a new results                                     |\n");
        printf("       |                                                             |\n");
        printf("       |  4.Display the match with the highest number of goal scored |\n");
        printf("       |                                                             |\n");
        printf("       |  5.Display all the matches of a team                        |\n");
        printf("       |                                                             |\n");
        printf("       |  6.Exit program                                             |\n");
        printf("       |                                                             |\n");
        printf("       |  Please enter 1, 2, 3, 4, 5 or 6                            |\n");
        printf("       |                                                             |\n");
        printf("       |_____________________________________________________________|\n");


        printf(" \n   Please Key In Your Option  " );
        scanf("%2s", &option);//%2s - error checking for white spaces and single digit
        //if i put %1s it recognise only first digit from double digit character, e.g: 
        // if you input 12 for option it will ignore 2 and swich program to case 1
        flushall();

        clrscr();  // Clears the screen

        num = atoi(&option);//convert char to int to perform error checking 

        //error checking for options (checks that input is not a letter)
        if(isdigit(option))//isdigit() checks if character is a digit in range 0-9
        {
            //Start of 'swich case' that will switch program from one option to another
            switch(num)
            {
                case 1: //when user entered option 1
                {
                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    //call function convert()
                    converted = convert(text,binary,single_match);
                    if(converted != 0)
                    {
                        printf("\n The text file matches.txt successfully converted to binary file matches.bin\n");
                    }
                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

                    printf("\n\n     *** Press ENTER to proceed to the MENU! ***\n");
                    getch(); //getch is used to hold the screen , if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                } //end of case 1

                case 2: //when user entered option 2
                {
                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    // call function  league_table();
                    league_table(binary,single_match,team_array);

                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

                    printf("\n\n     *** Press ENTER to proceed to the MENU! ***\n");
                    getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                } //end of case 2

                case 3: //when user entered option 3
                {
                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    //call function new_results()
                    new_results(binary,single_match);

                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

                    printf("\n\n     *** Press ENTER to proceed to the MENU! ***\n");
                    getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                }//end of case 3

                case 4: // when user entered option 4
                {
                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    //call function display_highest()
                    display_highest(binary,single_match); 

                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

                    printf("\n\n     *** Press ENTER to proceed to the MENU! ***\n");
                    getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                }//end of case 4

                case 5: // when user entered option 5
                {
                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
                    //call function display_all()
                    dislay_all(binary,single_match); 

                    printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

                    printf("\n\n     *** Press ENTER to proceed to the MENU! ***\n");
                    getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                }//end of case 5

                case 6://case 6 when user entered option 6for exit
                {
                    exit();

                    getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                }//end of case 6

                default://error checking: if input is not single digit in range 1-6 it will display following: 
                {
                    printf("\n   There is no option %d!",num);
                    printf("\n   Error In Input! \n");
                    printf("\n   Please Try Again! \n");

                    printf("\n\n     *** Press ENTER to proceed to the MENU! ***\n");
                    getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
                    clrscr();  // Clears the screen

                    break;
                }//end of default

            }//end switch

        }//end of if()
        else
        {
            //error checking: if input is a letter it will display following: 
            printf("\n   There is no option %c!",option);
            printf("\n   Error In Input!!! \n");
            printf("\n   Please Try Again! \n");

            printf("\n\n*** Press ENTER to proceed to the MENU! ***\n");
            getch(); //getch is used to hold the screen in simple language, if u don't write this the screen
                            //will just flash and go away
            clrscr();  // Clears the screen

        } // end of else - error checking for options

  }//end do 
  while(num != 6); //while user entered option 5 - exit()

    getchar();
}

/* Implement convert() */
int convert(FILE *text,FILE *binary, struct match single_match)
{
    int count = 0;
    int check;

    // Open the source file for reading
    if((text=fopen("matches.txt","r"))==NULL)
    {
        printf("\n open read file error.\n");
        exit(1);
    }
    // Open the destination file for writing
    if((binary=fopen("matches.bin","wb"))==NULL)
    {
        printf("\n open write file error.\n");
        exit(1);
    }
     // while not end of file for the source file
    while(fscanf(text, "%[^,],%[^,],%d,%d",single_match.team_A, single_match.team_B, &single_match.goals_A, &single_match.goals_B)!= EOF)
    {    
        count = fwrite(&single_match,sizeof(single_match),1,binary);   //write 1 structure
        if(count == 0)
        {               //check if any error writing
            printf("\n write file error.\n");
            check = 0;
            exit(1);
        }
        else
        {
            check = 1;
        }
    }   

    fclose(text);
    fclose(binary);
    return(check);
}//end convert()

/*Implement  league_table()*/
void league_table(FILE *binary,struct match single_match,team_no team_array[])
{
    struct match buffer[SIZE];
    int read = 0;
    int count=0;
    int i,j;
    int check;
    int numA,numB;
    int scored[10]={0};
    int conc[10] = {0};
    int points[10] = {0};
    printf("\n   League Table   \n");
    printf("________________________________________________________________________________\n");
    printf("--------------------------------------------------------------------------------\n");

    i = 0;
    //read the binary file and copy teams name to the team struct
    if((binary = fopen("matches.bin","rb"))!= NULL)
    {
        while(feof(binary) == 0 && i<180)
        {
            read = fread(&single_match,sizeof(struct match),1,binary);
            if(read == 0)
            {
                printf("\n Error in reading matches.bin");
            }
            else
            {
                //printf(" %s %s %d %d",single_match.team_A, single_match.team_B, single_match.goals_A, single_match.goals_B);

                strcpy(buffer[i].team_A, single_match.team_A);
                strcpy(buffer[i].team_B, single_match.team_B);
                buffer[i].goals_A = single_match. goals_A;
                buffer[i].goals_B = single_match. goals_B;
                i++;
            } 
        }
    }     
    else
    {
        printf("\n Error in opening binary file to read!!!");
        exit(1);
    }  
    fclose(binary);

    i = 0;

     //copy teams names to the struct team
    for(i = 0; i<10; i++)
    {
        strcpy(team_array[i].name,buffer[i].team_A);
        team_array[i].number = i + 1;
       // printf(" %s %d ",team_array[i].name, team_array[i].number);
    }

     // count points ,scored goals and conceded goals
    for(i = 0; i < 10; i++)
    {
        for(j = 0; j<SIZE; j++)
        {
            check = strcmp(team_array[i].name,buffer[j].team_A);
            if(check == 0)
            {
                numA = buffer[j].goals_A;
                scored[i] = scored[i] + numA;
                numB = buffer[j].goals_B; 
                conc[i] =  conc[i] + numB;
                count++;        
                if(buffer[j].goals_A > buffer[j].goals_B)
                {
                    points[i] = points[i] + 3;
                }
                if(buffer[j].goals_A == buffer[j].goals_B)
                {
                    points[i] = points[i] + 1;
                }
                else
                {
                   points[i] = points[i] + 0; 
                }
                //printf("\n %s   %s  %d  %d ",buffer[j].team_A, buffer[j].team_B, buffer[j].goals_A, buffer[j].goals_B);
            }  
            else
            {
                check = strcmp(team_array[i].name,buffer[j].team_B);
                if(check == 0)
                {
                    numB = buffer[j].goals_B;
                    scored[i] = scored[i] + numB;
                    numA = buffer[j].goals_A; 
                    conc[i] =  conc[i] + numA;
                    count++;

                    if(buffer[j].goals_A < buffer[j].goals_B)
                    {
                       points[i] = points[i] + 3;
                    }
                    if(buffer[j].goals_A == buffer[j].goals_B)
                    {
                        points[i] = points[i] + 1;
                    }
                     printf("\n %s   %s  %d  %d ",buffer[j].team_A, buffer[j].team_B, buffer[j].goals_A, buffer[j].goals_B);
                } 
            }                
        }
        printf("   %d |", count);
        printf("   %d", scored[i]);
        printf("   %d", conc[i]);
        printf("   %d\n\n",points[i]);
        count = 0;   
    }  

    i = 0;
    for(i = 0; i<10; i++)
    {
        team_array[i].scored = scored[i];
        team_array[i].conceded = conc[i];
        team_array[i].points = points[i];
        //printf("  %d",team_array[i].number);
        printf("%s ",team_array[i].name);
        printf("\t%i  ",team_array[i].scored);  
        printf("\t%i  ",team_array[i].conceded);
        printf("\t%i  \n",team_array[i].points);
    }

}//end league_table()

/*Implement new_results()*/
void new_results(FILE *binary,struct match single_match)
{
    int check;

    if((binary = fopen("matches.bin","ab"))!= NULL)
    {
        printf("\n Please enter the new team A:");
        gets(single_match.team_A);
        flushall();

        printf("\n Please enter the new team B:");
        gets(single_match.team_B);
        flushall();

        printf("\n Please enter the new Goals A:");
        scanf("%d",&single_match.goals_A);
        flushall();

        printf("\n Please enter the new Goals B:");
        scanf("%d",&single_match.goals_B);
        flushall();

        fseek(binary,0,SEEK_END);
        check = fwrite(&single_match,sizeof(single_match),1,binary);
        if(check == 0)
        {
            printf("Error writing to the file!!!");
        }
        fflush(binary);
    }
    else
    {
        printf("\n Error in opening binary file to read!!!");
        exit(1);
    }  
    fclose(binary);

    if((binary = fopen("matches.bin","rb"))!= NULL)
    {
        fseek(binary,-1*sizeof(single_match),SEEK_END);
        check = fread(&single_match,sizeof(struct match),1,binary);
        if(check == 0)
        {
            printf("\n Error in reading matches.bin");
        }
        else
        {
            printf("\n The new teams with the new results are:");
            printf(" %s %s %d %d",single_match.team_A, single_match.team_B, single_match.goals_A, single_match.goals_B);
        }
    }
    else
    {
        printf("\n Error in opening binary file to read!!!");
        exit(1);
    }  
    fclose(binary);

}//end new_results()

/*Implement display_highest()*/
void display_highest(FILE *binary,struct match single_match)
{
    struct match highest_goal;
    int read;
    int highest = 0;
    int total = 0;
    int numA;
    int numB;
    int i = 0;

    if((binary = fopen("matches.bin","rb"))!= NULL)
    {      
        while(feof(binary) == 0 && i < SIZE)
        {
            read = fread(&single_match,sizeof(struct match),1,binary);
            if(read == 0)
            {
                printf("\n Error in reading matches.bin");
            }
            else
            {
                numA = single_match.goals_A;
                numB = single_match.goals_B;
                total = numA + numB;
                if(highest < total)
                {
                    highest = total;
                    strcpy(highest_goal.team_A, single_match.team_A);
                    strcpy(highest_goal.team_B, single_match.team_B);
                    highest_goal.goals_A = single_match. goals_A;
                    highest_goal.goals_B = single_match. goals_B;
                }
            }
            i++;
        }
        printf("\nThe match with the highest number of goal scored is:   %s  %s  %d  %d",highest_goal.team_A, highest_goal.team_B, highest_goal.goals_A, highest_goal.goals_B);
    }
    else
    {
        printf("\n Error in opening binary file to read!!!");
        exit(1);
    }  
    fclose(binary);

}//end display_highest()

/*Implement dislay_all() */
void dislay_all(FILE *binary,struct match single_match)
{
    char name[16] = " ";
    int read;
    int i = 0;
    int count = 0;
    int check = 0;

    if((binary = fopen("matches.bin","rb"))!= NULL)
    {   
        printf("Please enter the name of the team all matches of which you want to display:\n");
        gets(name);

        fseek(binary,0,SEEK_SET);

        while(feof(binary) == 0 && i < SIZE)
        {
            read = fread(&single_match,sizeof(struct match),1,binary);
            if(read == 0)
            {
                printf("\n Error in reading matches.bin");
            }
            else
            {
                check++;
                if(strcmp(name,single_match.team_A) == 0)
                {   
                    printf("\n %s %s %d %d",single_match.team_A, single_match.team_B, single_match.goals_A, single_match.goals_B);
                    count++;

                    if(strcmp(name,single_match.team_B) == 0)
                    {
                       printf("\n %s %s %d %d",single_match.team_A, single_match.team_B, single_match.goals_A, single_match.goals_B);
                       count++;
                    } 
                } 
            }
            i++;
        }
        printf(" %d   %d", check, count);
        if(count == 0)
        {
            printf("The team %s is not found", name);
        }
    }
    else
    {
        printf("\n Error in opening binary file to read!!!");
        exit(1);
    }  
    fclose(binary);

}//end  dislay_all() 

  /* Implement exit( )  */
void exit(void)
{
   printf("\n\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");
   printf("\n\n   Thank you for using our service.\n   Press ENTER to exit the program.\n   Bye! "  );
   printf("\n\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *");

}//end exit()

Part of the problem lies in trying to write out the structs directly, rather than writing the individual fields out in an system-independent form (a process called serialization). A struct is not necessarily straight data; there may be padding for architecture-specific memory alignment, for example, and if the struct contains a pointer, it may hold a direct reference to an object in memory which won't necessarily be in the same location when the struct is reconstituted, or even exist at all anymore.

While this particular struct should be writeable, it is better to serialize the data rather than just assume that you can write out and read back the data structure as a whole. So, instead of

count = fwrite(&single_match,sizeof(single_match),1,binary);

you would be better off writing

char team_a[20], team_b[20];
int goals_a, goals_b;   /* at the start of the function */
/* ...  */

memcpy(team_a, single_match.team_a);
count = fwrite(team_a, sizeof(char), 20, binary);
check = check_write(count);   /* a utility function that does the write checking */
memcpy(team_b, single_match.team_b);
count = fwrite(team_b, sizeof(char), 20, binary);
check = check_write(count);
goals_a = single_match.goals_a;
count = fwrite(&goals_a, sizeof(int), 1, binary);
check = check_write(count);
goals_b = single_match.goals_b;
count = fwrite(&goals_b, sizeof(int), 1, binary);
check = check_write(count);    
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.