Parsing CSV file into Structs using C

2.3k views Asked by At

I am trying to parse a CSV File and put values into a struct, but when I exit my loops I only get returned the value of the file. I can't use strtok because some of the values in the csv file are empty and they get skipped over. My solution was strsep, and I can print all the songs while I'm inside the first while loop but when I leave it just returns me the last value of the file.

#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;
 }

Can someone please tell me why this is happening and how to fix it?

1

There are 1 answers

5
Pras On BEST ANSWER

you should change newSong.title = Token; to strcpy(newSong.title,Token); as Token is pointing to one of the addresses from buffer which is going to hold new data when next line is read

Also it will avoid memory leak

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

Will only allocate sizeof(char *) bytes so you have allocated less bytes than what you need, that could lead to segmentation if token is more than 4 or 8 bytes depending on sizeof(char *) is on your system

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

Change it to

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