reading a csv file into struct array

前端 未结 3 1464
时光说笑
时光说笑 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:50

    In the while loop:

    while (fgets(buf, 255, bookFile) != NULL)
    

    you are copying into the memory location of buffer new contents from file. As tmp points to a certain point in the buffer, its contents are being replaced too.

     tmp = strtok(NULL, ";");
     books[i].name = tmp;
    

    You should allocate memory for each struct of the array and then use strcopy.

    You can find an explanation of differences between strcpy and strdup here: strcpy vs strdup

提交回复
热议问题