0

我正在尝试用 C 编写一个 csv 解析器,但每次我在这个函数中遇到一个段错误。我不知道如何解决它。

char*** csv_loader(char *filename){
FILE* file_xx;
file_xx = fopen(filename, "r");
if(file_xx==NULL){
    printf("Failed to open File, no such file or directory!\n");
    return 0;
}
int c_=0;
int **linenumbers;
int l=lines(filename);
linenumbers=malloc(sizeof(int)*l);
char*** loaded_csv;
int counter_line=0;
int counter_row=0;
loaded_csv=malloc(sizeof(char **) *l);
loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);
if(NULL==loaded_csv){
    printf("Failed to initialize 'char** loaded_csv'!\n");
    return 0;
}
int c_c=0;
int *cm=get_column_map(filename);
for(c_c=0;c_c<l;c_c++){
    loaded_csv[c_c]=malloc(sizeof(char *)*cm[c_c]);
}
while(c_!=EOF){
    c_=getc(file_xx);
    if(c_=='\n'){
        linenumbers[counter_line][cm[counter_line]]=counter_row+2;
        loaded_csv[counter_line][cm[counter_line]]=malloc(counter_row*sizeof(char));
        if(NULL == loaded_csv[counter_line][cm[counter_line]]){
        return 0;
        }
        loaded_csv[counter_line][counter_row]='\0';
        counter_row=0;
        counter_line++;
    }else{
        if(c_==','){
            counter_row=0;
        }else{
            counter_row++;
        }
    }
}
fclose(file_xx);
FILE*fgetsread;
fgetsread=fopen(filename, "r");
int ident, ident_c;
for(ident=0;ident<l;ident++){
    for(ident_c=0;ident_c<cm[ident];ident_c++){
        fgets(loaded_csv[ident][ident_c], linenumbers[ident][ident_c], fgetsread);
        loaded_csv[ident][ident_c][linenumbers[ident][ident_c]-2]='\0';
    }
}
fclose(fgetsread);
free(linenumbers);
return loaded_csv;
}

调试器说这是这一行:

    loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);

有谁知道是什么错误?我还是 C 的新手,无论如何尝试理解 malloc 的东西......

PS:其他功能在这里: http: //pastebin.com/VQZ4d5UU

4

2 回答 2

1

因此,您之前已经在该行上分配了空间:

loaded_csv=malloc(sizeof(char **) *l);

这很好,很花哨,但loaded_csv[0]还没有初始化到你拥有的某个地方。因此,当您执行以下操作时

loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);

您正在尝试设置位于某个随机位置的变量(loaded_csv[0]当时恰好在哪里)。

如果你想触摸loaded_csv[0][0],你必须确保它loaded_csv[0]首先指向有效的内存(可能是malloc 你为它分配一些东西之前为它分配内存loaded_csv[0][0]。)

于 2013-08-30T17:36:17.667 回答
0

这条线有问题:

loaded_csv[0][0]=malloc(getfirstcolumn(filename)*sizeof(char)+2);  

这表明您没有正确分配和初始化loaded_csv[0][0]。

以下是如何初始化和使用 char ***var 的示例:

#include <windows.h>
#include <ansi_c.h>
char *** Create3D(int p, int c, int r);
int main(void)
{
    char ***pppVar;

    pppVar = Create3D(10,10,10);  
    //test pppVar;
    strcpy(pppVar[0][0], "asdfasf");
    strcpy(pppVar[0][1], "the ball");

    return 0;
}   

char *** Create3D(int p, int c, int r) 
{
    char *space;
    char  ***arr;
    int    x,y;

    space = calloc (p*c*r*sizeof(char),sizeof(char));
    arr = calloc(p * sizeof(char **), sizeof(char));
    for(x = 0; x < p; x++)
    {
        arr[x] = calloc(c * sizeof(char *),sizeof(char));
        for(y = 0; y < c; y++)
        {
            arr[x][y] = ((char *)space + (x*(c*r) + y*r));
        }
    }
    return arr;
}  

确保适当地跟踪和释放(x)。

于 2013-08-30T18:10:14.763 回答