The program i am working on creates a file (about.txt) which includes a highscore section.
On line 12 of the .txt is…
Plain Text (With no highscore):
- with <0>
C:
fprintf(about,"-%s with <%ld>",highname,highscore);
I need to read the score from the file and test to see if it is larger than the current highscore before writing the new one.
i need…
if(score > highscore)
highscore=score;
The only problem is how do i get highscore from the file.
I did some research myself and im sure that this is much easier than i am making it but when i looked around i couldnt find any way to do this.
Thank you.
/////////////////////////////////EDIT////////////////////////
Creating the file:
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);
...
Getting Scores:
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);
Changing the variables (note.. highscore/highname are global variables)
if(score >= highscore) //alter highscore
{
highscore = score;
highname = name;
puts("NEW HIGHSCORE!!!\n");
}
I get the error:
error: incompatible types when assigning to type 'char[3]' from type 'char'
On this line:
highname = name;
Name/score/highname/highscore declared here(in a header file):
char name[3];
char highname[3];
long score;
long highscore;
You can use
fscanf‘s little known but very powerful regex feature, together with its ability to skip entries based on regular expressions:Open the file, and skip the first 11 lines in a loop. Then read the score, like this:
This will skip everything in the file up to the opening
<bracket, then skip the bracket itself, and read an integer entry. Note that%*in the format string designates an entry to be skipped byfscanf. Here is a snippet at ideone.EDIT — In response to the additional question from your edit: you cannot assign arrays like that, you should use
memcpyinstead: