使用C将CSV文件解析为Structs [英] Parsing CSV file into Structs using C

查看:41
本文介绍了使用C将CSV文件解析为Structs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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

还可以避免内存泄漏

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

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

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);

将其更改为

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

这篇关于使用C将CSV文件解析为Structs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆