使用 C 将 CSV 文件解析为结构

Parsing CSV file into Structs using C

我正在尝试解析 CSV 文件并将值放入结构中,但是当我退出循环时,我只返回了文件的值。我不能使用 strtok,因为 csv 文件中的某些值是空的,它们会被跳过。我的解决方案是 strsep,当我在第一个 while 循环中时,我可以打印所有歌曲,但是当我离开它时,我只是 returns 文件的最后一个值。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "apue.h"

#define BUFFSIZE 512

typedef struct song{
    char *artist;
    char *title;
    char *albumName;
    float duration;
    int yearRealeased;
    double hotttness;
} song;

typedef song *songPtr;

 int main(){
     FILE *songStream;
     int count = 0;
     int size = 100;
     char *Token;

     char *buffer;
     song newSong;
     songPtr currentSong;
     songPtr *allSongsArray = malloc(size * sizeof(songPtr));
     buffer = malloc(BUFFSIZE+1);

     songStream = fopen("SongCSV.csv", "r");

     if(songStream == NULL){
         err_sys("Unable to open file");
     }else{
         printf("Opened File");
             while(fgets(buffer, BUFFSIZE, songStream) != NULL){
                 char *line = buffer;
                 int index = 0;

                 while ((Token = strsep(&line,","))) {
                     if(index == 17){
                         newSong.title = malloc(sizeof(Token));
                         newSong.title = Token;
                     }
                     index++;
                 }
                 currentSong = malloc(1*sizeof(song));
                 *currentSong = newSong;
                 allSongsArray[count] = malloc(sizeof(currentSong) + 1);
                 allSongsArray[count] = &newSong;
                 free(currentSong);
                 printf("\n%s\n", allSongsArray[count]->title);
                 count++;

                 if(count == size){
                     size = size + 100;
                     allSongsArray = realloc(allSongsArray ,size * sizeof(songPtr));
                 }
             }
             fclose(songStream);
     }

     fprintf(stdout,"Name in struct pointer array:  %s\n",allSongsArray[2]->title);


     return 0;
 }

谁能告诉我为什么会这样以及如何解决它?

您应该将 newSong.title = Token; 更改为 strcpy(newSong.title,Token); 因为 Token 指向缓冲区中的地址之一,该地址将在读取下一行时保存新数据

也可以避免内存泄漏

newSong.title = malloc(sizeof(Token));

将只分配 sizeof(char *) 字节,因此您分配的字节少于您需要的字节,如果令牌超过 4 或 8 个字节,这可能导致分段,具体取决于您系统上的 sizeof(char *)

             *currentSong = newSong; 
             allSongsArray[count] = malloc(sizeof(currentSong) + 1); 
             allSongsArray[count] = &newSong;
             free(currentSong);

改为

memcpy(currentSong,&newSong,sizeof(newSong));
allSongsArray[count] = currentSong;