reading a csv file into struct array

前端 未结 3 1459
时光说笑
时光说笑 2021-01-15 13:29

I\'m beginning to code in C. My code is as follows:

#include 
#include 
#include 

#define MAX_STR_LEN 256
#de         


        
3条回答
  •  温柔的废话
    2021-01-15 13:37

    The problem is your struct:

    struct book{
        int ID;
        char *name;
        char *dateIn;
        char *dateOut;
    };
    

    name, dateIn, dateOut are "pointer", they are just point to something, you're not allocating spaces for them.
    What you do is just point them to tmp(buf).

    So what you do in printBookList() is just print same string block, while ID is OK since it's not pointer.

    To solve this, allocate space for them, you can use strdup(), but make sure to free them.

提交回复
热议问题