您好我正在尝试创建一个函数,它将以字符串搜索形式从用户那里获取包含足球结果的二进制文件,例如(Chelsea,Liverpool,1,2),并在每次出现字符串时打印例如,如果用户进入利物浦,则所有利物浦的结果将被打印到屏幕上。我的代码不会打印任何内容。我究竟做错了什么? 这就是我所拥有的。
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);
}
答案 0 :(得分:0)
从
改变while(feof(file1) == 0)
到
while(feof(file1) != 0)
这可能是个问题。