简体   繁体   English

从文件读取数据并读入结构

[英]Reading data from a file and into a struct

I'm trying to read in data from a file and store the data in a struct. 我正在尝试从文件中读取数据并将数据存储在结构中。 The struct looks like this: 该结构如下所示:

struct someData {
    int number;
    char *name;
};

The data in the file can be any format. 文件中的数据可以是任何格式。 Right now I'm trying to just read in CSV format. 现在,我正在尝试仅以CSV格式阅读。 The data will always be a string followed by an integer. 数据将始终是字符串,后跟整数。

some string data,100
another string,500

Here is the part of the code where I'm trying to read the data and place it in the struct. 这是我尝试读取数据并将其放置在结构中的代码部分。

FILE *ifp;
char *mode = "r";
char *name;
int number;

ifp = fopen("myDataFile.txt", mode);

if (ifp == NULL) {
    fprintf(stderr, "Can't open input file in.list!\n");
    exit(1);
}

// read up to 100 characters up to a comma, then a decimal
while(fscanf(ifp, "%100[^,],%d\n", name, &number) != EOF){ 

    // print out the data we got 
    printf("Data from file:  %s  %d", name, number);


    struct someData *newData = (struct someData *) malloc(sizeof(struct someData));
    newData->number = number;
    newData->name = name;
    printf("Name: %s  Number: %d\n\n",newData->name, newData->number);
    } 

The print statements give me the following 打印声明给我以下内容

Data from file: some string d 100
Name: some string d  Number: 100

Data from file: another stri  Number: 500
Name: another stri  Number 500

The second name gets cut off and actually ends up printing a couple of weird characters. 第二个名字被切断,实际上最终打印出几个奇怪的字符。 I think something is wrong with my fscanf in the while loop. 我认为while循环中的fscanf有问题。 I've tried a few other ways to get the data like using %s instead of %100 but nothing is working. 我尝试了其他几种获取数据的方法,例如使用%s代替%100,但没有任何效果。

You need to change the second char *name; 您需要更改第二个 char *name; to char name[101]; char name[101]; (not the one in the struct ). (不是struct中的那个)。

And it would be better if you changed: 如果您进行以下更改会更好:

    while(fscanf(ifp, "%100[^,],%d\n", name, &number) != EOF){

to

    while(fscanf(ifp, " %100[^,],%d", name, &number) == 2){

This: 这个:

  • Adds skipover of initial white space 添加初始空白的跳过
  • Removes trailing \\n , which can sometime cause problems 删除尾随\\n ,这有时会导致问题
  • Verifies that two valid arguments have been read. 验证是否已读取两个有效参数。

Also newData->name = name; 也是newData->name = name; won't work: you'll need something like newData->name = strdup(name); 将无法正常工作:您将需要类似newData->name = strdup(name); .

Also, as a matter of style, I would suggest changing: 另外,作为样式,我建议更改:

     struct someData *newData = (struct someData *) malloc(sizeof(struct someData));

to: 至:

     struct someData *newData = malloc(sizeof *newData);

which will avoid some redundancy ie "DRY". 这样可以避免一些冗余,即“ DRY”。

And you never save newData ; 而且您永远不会保存newData I presume you are planning to add it to a linked-list or some other such container. 我想您打算将其添加到链接列表或其他此类容器中。

Also, you'll eventually need to add error-checking, but that is beyond the scope of this question. 另外,您最终将需要添加错误检查,但这超出了此问题的范围。

You are using not-initialised pointer node. 您正在使用未初始化的指针节点。 The function fscanf requires for strings some buffer, and you do not provide any. fscanf函数需要为字符串提供一些缓冲区,而您不提供任何缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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