int main() {
FILE *fp = fopen("fileA.txt", "r"); /* read file */
int i = 0;
char name[200][100];
char goods[200][100];
char qty[200][100];
char temp[200][100];
int x = 0;
int result;
while (!feof(fp)) {
fscanf(fp, "%[^,] , %[^,] , %s " , name[i], item[i], qty[i]); /*get file content and store in array */
if (strcmp(item[i], "Football") == 0) { /* only select Football */
temp[x][x] = qty[i];
if (x > 0) {
if (strcmp(temp[x][x], temp[x + 1][x + 1]) > 0) { /*compare who has more football qty */
result = x; /*output the person who have more football*/
}
}
x = x + 1;
}
}
printf("%s is team leader in class.\n", name[result]);
fclose(fp);
getchar();
return 0;
}
大家好,我不知道为什么结果不正确。
我想找出谁拥有更多的足球并打印出他/她的名字。
if (strcmp(temp[x], temp[x + 1]) > 0)
我不清楚使用指针和地址似乎有问题。
文本文件中的内容是:
Alice,Eating,001
Kitty,Football,006
Ben,Swimming,003
May,Football,004
我希望结果是:
Kitty is team leader in class.
谢谢你。