排序贪婪高分(文件I / O)

时间:2015-05-24 18:21:05

标签: c sorting file-io

我正在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);
   }
}

1 个答案:

答案 0 :(得分:0)

您需要的是一个用于存储玩家及其分数的数据结构。 E.g:

struct Player {
    const char* name;
    int score;
};

然后你可以创建一个这样的结构数组,将文件中的所有玩家数据放在那里,然后应用qsort()对它进行排序。您还需要一个比较功能,按分数命令玩家提供给qsort。