C 在文件中保存最高分
所以我想保存游戏中最好的3分,并将它们放在一个文件中。但由于某种原因,当我阅读文件时,最好的分数是53,32,32。现在不用名字了,只有3分。而且我对文件也不熟悉C 在文件中保存最高分,c,C,所以我想保存游戏中最好的3分,并将它们放在一个文件中。但由于某种原因,当我阅读文件时,最好的分数是53,32,32。现在不用名字了,只有3分。而且我对文件也不熟悉 typedef struct score{ unsigned char score[3]; //char name[20]; } SCORES; 这就是我储蓄的方式 void guardar_highscore (SCORES top){ FILE *f; f = fopen ("/var/www/html/hi
typedef struct score{
unsigned char score[3];
//char name[20];
} SCORES;
这就是我储蓄的方式
void guardar_highscore (SCORES top){
FILE *f;
f = fopen ("/var/www/html/highscore3.txt","wb");
if (f ==NULL)
perror ("nope2"),exit (1);
fprintf(f,"%d \n %d \n %d \n",top.score[0],top.score[1],top.score[2]);
fclose(f);
}
这就是我读给结构的方式
SCORES ler_highscore (){
SCORES top={0};
int i=0;
char line[20];
FILE *f;
f = fopen ("/var/www/html/highscore3.txt","rb");
if (f ==NULL)
perror ("nope"), exit (1);
while(fgets(line,20, f) != NULL){
sscanf (line, "%c", &top.score[i]);
i++;
}
fclose(f);
return top;
}
分数通常是数字,所以将它们存储为单个字符没有多大意义。当你注意到你正在把它们写成整数(并且有一个额外的空格)时,问题就清楚了
但你把它们当作角色来读
sscanf (line, "%c", &top.score[i]);
53、32、32看起来很像5
和两个空格的ASCII数字。如果你把字符5
写成一个数字,你会得到53。这是因为字符5
是数字53。看看为什么
解决方法是一致地使用整数
typedef struct {
int score[3];
} Scores;
请注意,ALL_CAPS
通常为常量而不是类型保留
guardar_highscore
基本保持不变,尽管我已经清理了一些
// The filename is now a variable so its used consistently
// and can be used in error messages.
const char Score_File[] = "highscore3.txt";
void guardar_highscore(const Scores *top) {
FILE *fd = fopen (Score_File,"wb");
if (fd == NULL) {
// A more informative error message than "nope".
fprintf( stderr, "Could not open '%s' for writing: %s\n", Score_File, strerror(errno) );
exit(1);
}
// Loop instead of repeating the formatting. This makes adding more
// scores easier.
// Note the stray whitespace is gone.
for( int i = 0; i < 3; i++ ) {
fprintf(fd, "%d\n", top->score[i]);
}
fclose(fd);
}
分数通常是数字,所以将它们存储为单个字符没有多大意义。当你注意到你正在把它们写成整数(并且有一个额外的空格)时,问题就清楚了
但你把它们当作角色来读
sscanf (line, "%c", &top.score[i]);
53、32、32看起来很像5
和两个空格的ASCII数字。如果你把字符5
写成一个数字,你会得到53。这是因为字符5
是数字53。看看为什么
解决方法是一致地使用整数
typedef struct {
int score[3];
} Scores;
请注意,ALL_CAPS
通常为常量而不是类型保留
guardar_highscore
基本保持不变,尽管我已经清理了一些
// The filename is now a variable so its used consistently
// and can be used in error messages.
const char Score_File[] = "highscore3.txt";
void guardar_highscore(const Scores *top) {
FILE *fd = fopen (Score_File,"wb");
if (fd == NULL) {
// A more informative error message than "nope".
fprintf( stderr, "Could not open '%s' for writing: %s\n", Score_File, strerror(errno) );
exit(1);
}
// Loop instead of repeating the formatting. This makes adding more
// scores easier.
// Note the stray whitespace is gone.
for( int i = 0; i < 3; i++ ) {
fprintf(fd, "%d\n", top->score[i]);
}
fclose(fd);
}
回复:“本场比赛的最佳得分”,什么比赛?Re:“当我阅读文件时,最好的分数是53,32,32”,这些是最好的分数吗?您希望得到什么?为什么将分数存储为
无符号字符
?int
更合适。你把分数写成一个整数,但是当你把它们读回时,你把它们作为一个字符来读。这不会顺利的。您可能得到了ASCII值,32是空格,53是5。@coreyward这是一个使用html和C的类似流氓的游戏。53,32,32不是最好的分数,我不知道它们代表什么,因为当我第一次运行它时,它应该是0,0,0。。。我希望能够将分数从文件读取到结构,用我将获得的分数修改结构值,然后将其写回文件。@Schwern由于分数的范围,我使用的是unsigned char。我在玩Int,但当我得到127时,它又降到了0。@Schwern它现在正在使用Int,它正在做我想要的,thx!回复:“本场比赛的最佳得分”,什么比赛?Re:“当我阅读文件时,最好的分数是53,32,32”,这些是最好的分数吗?您希望得到什么?为什么将分数存储为无符号字符
?int
更合适。你把分数写成一个整数,但是当你把它们读回时,你把它们作为一个字符来读。这不会顺利的。您可能得到了ASCII值,32是空格,53是5。@coreyward这是一个使用html和C的类似流氓的游戏。53,32,32不是最好的分数,我不知道它们代表什么,因为当我第一次运行它时,它应该是0,0,0。。。我希望能够将分数从文件读取到结构,用我将获得的分数修改结构值,然后将其写回文件。@Schwern由于分数的范围,我使用的是unsigned char。我在玩Int,但当我得到127时,它又降到了0。@Schwern它现在正在使用Int,它正在做我想要的,thx!