如何从文件中读取变量

时间:2021-05-06 23:30:51

The program i am working on creates a file (about.txt) which includes a highscore section.

我正在处理的程序创建了一个文件(about.txt),其中包含一个高分部分。

On line 12 of the .txt is...

在.txt的第12行是......

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)

更改变量(注意.. highscore / highname是全局变量)

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;

2 个解决方案

#1


0  

You can use fscanf's little known but very powerful regex feature, together with its ability to skip entries based on regular expressions:

您可以使用fscanf鲜为人知但非常强大的正则表达式功能,以及基于正则表达式跳过条目的功能:

Open the file, and skip the first 11 lines in a loop. Then read the score, like this:

打开文件,跳过循环中的前11行。然后读取分数,如下所示:

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.

这将跳过文件中的所有内容,直到开头 <括号,然后跳过括号本身,并读取整数条目。请注意,格式字符串中的%*指定fscanf要跳过的条目。这是ideone的一个片段。< p>

EDIT -- In response to the additional question from your edit: you cannot assign arrays like that, you should use memcpy instead:

编辑 - 响应编辑中的其他问题:您无法分配这样的数组,您应该使用memcpy:

memcpy(highname, name, 3);

#2


0  

You'll need to use fscanf to do that; it's a bit like the inverse of fprintf.

你需要使用fscanf来做到这一点;它有点像fprintf的反转。

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

请查看此处的文档:http://cplusplus.com/reference/clibrary/cstdio/fscanf/

#1


0  

You can use fscanf's little known but very powerful regex feature, together with its ability to skip entries based on regular expressions:

您可以使用fscanf鲜为人知但非常强大的正则表达式功能,以及基于正则表达式跳过条目的功能:

Open the file, and skip the first 11 lines in a loop. Then read the score, like this:

打开文件,跳过循环中的前11行。然后读取分数,如下所示:

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.

这将跳过文件中的所有内容,直到开头 <括号,然后跳过括号本身,并读取整数条目。请注意,格式字符串中的%*指定fscanf要跳过的条目。这是ideone的一个片段。< p>

EDIT -- In response to the additional question from your edit: you cannot assign arrays like that, you should use memcpy instead:

编辑 - 响应编辑中的其他问题:您无法分配这样的数组,您应该使用memcpy:

memcpy(highname, name, 3);

#2


0  

You'll need to use fscanf to do that; it's a bit like the inverse of fprintf.

你需要使用fscanf来做到这一点;它有点像fprintf的反转。

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

请查看此处的文档:http://cplusplus.com/reference/clibrary/cstdio/fscanf/