简体   繁体   中英

How to read a variable from a file

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'll need to use fscanf to do that; it's a bit like the inverse of fprintf.

Take a look at the documentation here: http://cplusplus.com/reference/clibrary/cstdio/fscanf/

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:

FILE *f = fopen("about.txt","r");
int i, score;
char buf[1024];
for (i = 0 ; i != 11 ; i++) {
    fgets(buf, 1024, f);
}
fscanf(f, "%*[^<]%*[<]%d", &score);
printf("%d\n", score);

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 by fscanf . 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 memcpy instead:

memcpy(highname, name, 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM