void Show() {
system("cls");
FILE *AddedSc;
int sc1i,sc2i;
int sc1ii,sc2ii;
char TNi[100],TN2i[100];
char TNii[100],TN2ii[100];
AddedSc = fopen("addedscores.txt", "r");
printf(" - Choose teams from this list: \n\n");
printf(" 1. Brazil\n 2. Germany\n 3. Italy\n 4. KSA\n 5. Portogual\n 6. Australia\n 7. USA\n 8. Spin\n 9. England\n 10. Korea\n\n\n");
printf(" * Enter the two teams that you want to show their results: ");
printf("\n\n\nTeam A: ");
scanf("%s", TNi);
printf("Team B: ");
scanf("%s", TN2i);
while (fscanf(AddedSc,"%[^\n] %d %d %[^\n]", TNii, sc1ii, sc2ii,TN2ii) !=EOF) {
if (strcmp(TNi, TNii) == 0 && strcmp(TN2i, TN2ii) == 0)
printf("\n\n%s %d - %d %s", TNi, sc1ii, sc2ii, TN2i);
else if(strcmp(TN2i, TNii) == 0 && strcmp(TNi, TN2ii) == 0)
printf("\n\n%s %d - %d %s", TN2i, sc2ii, sc1ii, TNi);
else printf("Not found!");
}
fclose(AddedSc);
有一个文件包含这样的数据
usa 2 0意大利
我希望用户输入两个团队的名称,让程序在文件中搜索并比较用户输入的内容并在屏幕上打印匹配结果。
请帮助我......
答案 0 :(得分:0)
问题是第一种格式%[^\n]
,它会占用输入中的所有行。您可以按以下方式更改%s
:
while (fscanf(AddedSc,"%s %d %d %s", TNii, sc1ii, sc2ii,TN2ii) != EOF)
将结果比较为更安全:
while (fscanf(AddedSc,"%s %d %d %s", TNii, sc1ii, sc2ii,TN2ii) == 4)