English 中文(简体)
从档案中读取并储存成一系列的构件
原标题:Reading from file and storing into an array of structs
  • 时间:2012-05-15 16:31:16
  •  标签:
  • c
  • struct

我有简单的档案格式。

NAME|VALUE
NAME|VALUE
NAME|VALUE

我正试图将这些内容放在并储存在一系列的构件中。

struct data
{
    char* name;
    char* value;
};

现在,我知道阵列的规模是3个,因此我这样做了:

struct data pairs[3];

这里是我的法典,因为我试图从档案中读到:

char *tempVal;
int i =0;
    if(file != NULL)
        {
            char curLine [128];
            while(fgets(curLine, sizeof curLine, stockFile) != NULL)
            {   

                tempVal = strtok(curLine,"|");
                printf("i:%i
",i);
                pairs[i].name= tempVal;
                printf("name at pos %i is %s
",i, pairs[i].name);
                tempVal = strtok(NULL,"|");
                pairs[i].value= tempVal;
                printf("value at pos %i is %s
",i, pairs[i].value);
                ++i;
            }
            fclose(file);
        }

而每张印本都印成正文,然后,我尝试用这一文字印刷阵列。

int j
for(j = 0; j < 3; j++)
    {   

        printf("ENTRY# %iNAME:%sVALUE:%s

",j,pairs[j].name, pairs[j].value);
    }

劳教是一种微薄的杂乱,尝试了带有密码区块的餐,但可能无法做到。 然而,我很想知道,为什么在整整段期间,它显示了正确的东西,但在整段之后,它显示了所有三个有相同名称的阵列(第三个条目的价值是正确的,而第一个和第二个条目的价值是,价值领域含有第三个条目正确价值的一半)。

感谢!

最佳回答

<代码>strtok()的回升值将指curLine的一个要素,因此struct中的所有条目都将指curLine中一个内容,该内容与每个电话fgets(>)的对应内容(并且只对当前版本有效)。

请在<代码>while期间,尽可能使用<代码>strdup():

while(fgets(curLine, sizeof curLine, stockFile) != NULL)
{   
    tempVal = strtok(curLine,"|");
    printf("i:%i
",i);
    pairs[i].name = strdup(tempVal);
    printf("name at pos %i is %s
",i, pairs[i].name);
    tempVal = strtok(NULL,"|");
    pairs[i].value = strdup(tempVal);
    printf("value at pos %i is %s
",i, pairs[i].value);
    ++i;
}

for (j = 0; j < 3; j++)
{
    free(pairs[j].name);
    free(pairs[j].value);
}
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签