I am trying to write a function to take input from user in the form of a struct
and insert it into a binary file. (of football results) the user is to enter teamA
, teamB
, goalsA
and goalsB
When I try to display what I have entered, I am not getting the correct result. What am I doing wrong?
Code:
void addResult()
{
struct matches match;
char input[100];
FILE * file1;
file1 = fopen("matches.bin","rw+b");
printf("Enter the resulst you want to add to the file\n");
scanf("%s",input);
while(!feof(file1))
fread(&match,sizeof(match),1,file1);
fseek(file1,sizeof(match),SEEK_END);
fwrite(&input,sizeof(match),1,file1);
fflush(file1);
fclose(file1);
file1 = fopen("matches.bin","r");
while(!feof(file1)) {
fread(&match,sizeof(match),1,file1);
printf("%s %s %i %i\n",match.teamA,match.teamB,match.goalsA,match.goalsB);
}
fclose(file1);
}