读取csv到struct

时间:2015-09-05 14:29:11

标签: c csv struct malloc

我正在尝试将textfile放到struct

将值从表单文本文件分配给结构时出现问题,它会不断打印我打印的最后一个值。

你能帮我解决这个问题吗?

提前谢谢。

#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN 422498
#define MAX_KEY 128
    struct record
    {
        char* a;
        char* b;
    };

    int main(int argc, char **argv){
        char* input=argv[1];
        char* output=argv[2];
        char* buff[MAX_LEN];


        char *delimiter = ";";


        //printf("%s\n",input);
        //printf("%s\n",output);

        FILE *fp;
        fp = fopen(input, "r"); //opening file*/

        if( fp == NULL )
           {
              perror("Error while opening the file.\n");
              exit(EXIT_FAILURE);
           }
           int rowsNum=0;
           struct record* recArr = malloc(3 * sizeof (struct record));
           //struct record recArr[4];
        //recArr=malloc(1 * 2*sizeof(char*));
           /*recArr[0].a="aa";
           recArr[0].b="bb";
           recArr[1].a="cc";
           recArr[1].b="dd";
           recArr[2].a="ee";
           recArr[2].b="ff";

        printf("%s\n", recArr[0].a);
        printf("%s\n\n", recArr[0].b);
        printf("%s\n", recArr[1].a);
        printf("%s\n\n", recArr[1].b);
        printf("%s\n", recArr[2].a);
        printf("%s\n\n", recArr[2].b);*/
        while (fgets(buff, MAX_LEN, (FILE*)fp)!=NULL)
       {       
            //recArr=realloc(recArr, (rowsNum+1) * 2*sizeof(char*));

            //char* Key = strtok(buff, delimiter);
            //char* Value = strtok(NULL, delimiter);

            recArr[rowsNum].a=strtok(buff, delimiter);
            recArr[rowsNum].b=strtok(NULL, delimiter);

            printf("%s\n", recArr[rowsNum].a);
            printf("%s\n\n", recArr[rowsNum].b);
            /*Key=NULL;
            Value=NULL;*/
            rowsNum++;
            printf("%d\n", rowsNum);
        }
        fclose(fp);

        int i;
        for (i=0;i<3;i++)
        {
            printf("%s\n",  recArr[i].a);
            printf("%s\n\n", recArr[i].b);
        }
        //printf("%d\n", rowsNum);

    }

1 个答案:

答案 0 :(得分:1)

#include <stdio.h>
#include <stdlib.h>
#include <string.h> // this is needed to use strtok function (and strlen, strcpy)
#define MAX_LEN 422498
#define MAX_KEY 128
    struct record
    {
        char* a;
        char* b;
    };

    int main(int argc, char **argv){
        char* input=argv[1];
        char* output=argv[2];
        char buff[MAX_LEN]; // buff should be char[MAX_LEN], not char*[MAX_LEN]


        char *delimiter = ";";


        //printf("%s\n",input);
        //printf("%s\n",output);

        FILE *fp;
        fp = fopen(input, "r"); //opening file*/

        if( fp == NULL )
           {
              perror("Error while opening the file.\n");
              exit(EXIT_FAILURE);
           }
           int rowsNum=0;
           struct record* recArr = malloc(3 * sizeof (struct record));
           //struct record recArr[4];
        //recArr=malloc(1 * 2*sizeof(char*));
           /*recArr[0].a="aa";
           recArr[0].b="bb";
           recArr[1].a="cc";
           recArr[1].b="dd";
           recArr[2].a="ee";
           recArr[2].b="ff";

        printf("%s\n", recArr[0].a);
        printf("%s\n\n", recArr[0].b);
        printf("%s\n", recArr[1].a);
        printf("%s\n\n", recArr[1].b);
        printf("%s\n", recArr[2].a);
        printf("%s\n\n", recArr[2].b);*/
        while (fgets(buff, MAX_LEN, (FILE*)fp)!=NULL)
       {       
            //recArr=realloc(recArr, (rowsNum+1) * 2*sizeof(char*));

            //char* Key = strtok(buff, delimiter);
            //char* Value = strtok(NULL, delimiter);

            //recArr[rowsNum].a=strtok(buff, delimiter);
            //recArr[rowsNum].b=strtok(NULL, delimiter);
            // you have to COPY the string
            char* a=strtok(buff, delimiter);
            char* b=strtok(NULL, delimiter);
            recArr[rowsNum].a = malloc(sizeof(char) * (strlen(a) + 1)); // +1 for terminating '\0'
            recArr[rowsNum].b = malloc(sizeof(char) * (strlen(b) + 1));
            strcpy(recArr[rowsNum].a, a);
            strcpy(recArr[rowsNum].b, b);

            printf("%s\n", recArr[rowsNum].a);
            printf("%s\n\n", recArr[rowsNum].b);
            /*Key=NULL;
            Value=NULL;*/
            rowsNum++;
            printf("%d\n", rowsNum);
        }
        fclose(fp);

        int i;
        for (i=0;i<3;i++)
        {
            printf("%s\n",  recArr[i].a);
            printf("%s\n\n", recArr[i].b);
        }
        //printf("%d\n", rowsNum);

        // you should free the allocated buffer
        for (i=0;i<3;i++)
        {
            free(recArr[i].a);
            free(recArr[i].b);
        }
        free(recArr);

    }
    应该包含
  • string.h以使用strtok:GCC对此发出了一些警告。
  • buff的类型不合适:它应该是char buff[MAX_LEN],而不是char* buff[MAX_LEN](有额外的星号)
  • 函数strtok修改给定的缓冲区并返回指向缓冲区中某处的指针。读取下一行时,缓冲区将被覆盖,之前读取的文本将丢失,除非将其复制到某处。出于这个原因,我添加了一些代码来复制读取的字符串并保存指向字符串复制的指针而不是buff中的某个位置。