我正在开发的程序会创建一个包含高分部分的文件(about.txt)。
.txt 的第 12 行是...
纯文本(没有高分):
- with <0>
C:
fprintf(about,"-%s with <%ld>",highname,highscore);
在写入新的分数之前,我需要从文件中读取分数并测试它是否大于当前的高分。
我需要...
if(score > highscore)
highscore=score;
唯一的问题是我如何从文件中获得高分。
我自己做了一些研究,我确信这比我做的要容易得多,但是当我环顾四周时,我找不到任何方法来做到这一点。
谢谢你。/////////////////////////////////编辑//////////////// //////// 创建文件:
FILE *about;
fpos_t position_name;
fpos_t position_score;
...
fprintf(about,"\n\nHIGHSCORE:\n\n");
fprintf(about,"-");
fgetpos(about,&position_name);
fprintf(about,"%s",highname);
fprintf(about,"with");
fgetpos(about,&position_score);
fprintf(about,"%ld",highscore);
fclose(about);
...
获得分数:
FILE *about;
about = fopen("about.txt","r");
fseek(about,position_name,SEEK_SET);
fscanf(about,"%s",highname);
fseek(about,position_score,SEEK_SET);
fscanf(about,"%ld",highscore);
fclose(about);
更改变量(注意.. highscore/highname 是全局变量)
if(score >= highscore) //alter highscore
{
highscore = score;
highname = name;
puts("NEW HIGHSCORE!!!\n");
}
我得到错误:
error: incompatible types when assigning to type 'char[3]' from type 'char'
在这条线上:
highname = name;
此处声明的名称/分数/高名称/高分数(在头文件中):
char name[3];
char highname[3];
long score;
long highscore;