简体   繁体   English

在C中将CSV文件读取到2D双数组

[英]Reading CSV file to 2D double array in C

I need to write a program to parse a large CSV file (approx. 2000*2000) in C and store in the form of a double[] [] array. 我需要编写一个程序来解析C中的大CSV文件(大约2000 * 2000),并以double [] []数组的形式存储。 I wrote a program, which seems to work for small files (i checked for a 4*4 csv file), but for large files it gives me incorrect results.(as in the number of rows and columns are wrong and the program crashes after that). 我写了一个程序,该程序似乎适用于小文件(我检查了4 * 4的csv文件),但是对于大文件,它给了我错误的结果(因为行数和列数错误,并且程序在崩溃后崩溃了)那)。

This is the code: 这是代码:

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

You say you data has 2000 columns but your fgets() reads at most 4999 characters. 您说数据有2000列,但fgets()最多读取4999个字符。 Isn't there a chance your data is wider than 4999 chars? 您的数据是否有可能超过4999个字符? You should probably check that each line read in ends with a newline (except perhaps the last line in the file). 您可能应该检查读入的每一行是否以换行符结尾(也许文件中的最后一行除外)。

As an aside, you don't need to reopen the file--just rewind() it. 顺便说一句,您不需要重新打开文件-只需rewind()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM