Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中读取CSV文件到二维双数组_C_Arrays_Csv - Fatal编程技术网

在C中读取CSV文件到二维双数组

在C中读取CSV文件到二维双数组,c,arrays,csv,C,Arrays,Csv,我需要写一个程序来解析C中的一个大CSV文件(大约2000*2000),并以双[]数组的形式存储。我写了一个程序,它似乎适用于小文件(我检查了一个4*4的csv文件),但对于大文件,它给了我错误的结果(因为行和列的数量是错误的,程序在那之后崩溃) 代码如下: #include<stdio.h> #include<stdlib.h> #include<string.h> int main (void) { int rowMaxIndex,columnM

我需要写一个程序来解析C中的一个大CSV文件(大约2000*2000),并以双[]数组的形式存储。我写了一个程序,它似乎适用于小文件(我检查了一个4*4的csv文件),但对于大文件,它给了我错误的结果(因为行和列的数量是错误的,程序在那之后崩溃)

代码如下:

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

int main (void)
{
    int rowMaxIndex,columnMaxIndex;
    double **mat;
    double *matc;
    int i,j,idx,len;
    char part[5000];
    char *token;
    char *temp;
    char *delim = ",";
    double var;
{
    FILE *fp;
    fp = fopen("X1_CR2_new1.csv","r");

    if(fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    // count loop
    rowMaxIndex = 0;
    columnMaxIndex = 0;
    while(fgets(part,5000,fp) != NULL){
        token = NULL;
        token=strtok(part,delim);
                    while(token != NULL){
                       if(rowMaxIndex==0)
                       {
                       columnMaxIndex++;}
                       token=strtok(NULL,delim);
        }
        rowMaxIndex++;
    }
    fclose(fp);

    printf("Number of rows is %d, and Number of columns is %d", rowMaxIndex, columnMaxIndex);
    // allocate the matrix

    mat = malloc(rowMaxIndex * sizeof(double*));

    for (i = 0; i < rowMaxIndex; i++)
    {
        mat[i] = malloc(columnMaxIndex * sizeof(double));
        }
        fclose(fp);
}
    // rewind the file to the beginning. The rewind(fp) wasnt working so closed and reopened file.

{
    FILE *fp;
    fp = fopen("X1_CR2_new1.csv","r");

    if(fp == NULL)
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    // read loop
    i = j = 0;
    while(fgets(part,5000,fp)!=NULL)
    {    
        token=strtok(part,delim);
        j=0;
        while (token != NULL){
              mat[i][j]=atof(token);
              //printf("\n %f", mat[i][j]);
              token=strtok(NULL,delim);
              j++;
          }
        i++;
    }
    printf("\n The value of mat 1, 2 is %f", mat[1][0]);  //print some element to check
    free(mat);
    fclose(fp);
}    

    return 0;
}
#包括
#包括
#包括
内部主(空)
{
int rowMaxIndex,columnMaxIndex;
双**垫;
双*matc;
int i,j,idx,len;
字符部分[5000];
字符*令牌;
字符*温度;
char*delim=“,”;
双重var;
{
文件*fp;
fp=fopen(“X1_CR2_new1.csv”,“r”);
如果(fp==NULL)
{
perror(“打开文件时出错。\n”);
退出(退出失败);
}
//计数循环
rowMaxIndex=0;
columnMaxIndex=0;
while(fgets(第5000部分,fp)!=NULL){
令牌=NULL;
代币=strtok(部分,delim);
while(令牌!=NULL){
如果(rowMaxIndex==0)
{
columnMaxIndex++;}
token=strtok(NULL,delim);
}
rowMaxIndex++;
}
fclose(fp);
printf(“行数为%d,列数为%d”,rowMaxIndex,columnMaxIndex);
//分配矩阵
mat=malloc(rowMaxIndex*sizeof(double*);
对于(i=0;i
您说您的数据有2000列,但您的
fgets()
最多读取4999个字符。您的数据是否有可能超过4999个字符?您可能应该检查读入的每一行是否以换行结束(可能除了文件中的最后一行)


另一方面,您不需要重新打开该文件,只需
倒带()
即可。

或者更简单地说,使用POSIX,它总是返回完整的一行,除非它无法分配足够的内存。谢谢。改为20000字符,现在可以正常工作了。但是,尝试了rewind(),但并没有返回到文件的开头。我不知道为什么。Getline未包含在我的默认包中,因此未使用。请尝试调试代码: