【发布时间】:2015-05-24 18:21:05
【问题描述】:
我正在用 C 语言实现 Greed(greedjs.com)。如何将这些数据从高到低排序,以及如何附加仅获得前十名玩家的分数?
Here's 它的样子。
到目前为止,这是我的代码:
void high_scores() {
int c;
char player_name[256];
FILE * fhigh_score;
fhigh_score = fopen("HIGH SCORES.txt", "a");
if (fhigh_score == NULL) {
printf("Error");
exit(1);
}
printf("Your Name:\t");
fgets(player_name, 256, stdin);
player_name[strlen(player_name) - 1] = 0;
fprintf(fhigh_score, "\t%s\t%d\n", player_name, SCORE);
fclose(fhigh_score);
fhigh_score = fopen("HIGH SCORES.txt", "r");
if (fhigh_score == NULL) {
printf("Error");
exit(1);
}
else if (fhigh_score) {
while ((c = getc(fhigh_score)) != EOF) //print file to terminal
putchar(c);
fclose(fhigh_score);
}
}
【问题讨论】:
-
不需要这行:'else if (fhigh_score) {' 因为前面的 'if' 已经处理了打开文件读取的任何错误
-
注意到了。但是我怎样才能按谁的得分最高对它们进行排序呢?