Fadoua
Fadoua

Reputation: 161

EXC_BAD_ACCESS when reading from csv file using fgets and sscanf

I am trying to read from a huge .csv file (about 100,000 lines). Using fgets, I extract the entire line then using sscanf, I read 21 int values inside the line. However, sscanf returns error EXC_BAD_ACCESS at line 758. I tried to increase the size for the buffer and can read more lines but not all. Is there more elegant and clean way to read huge data with C? Thank you.

    char buffer[316]; // buffer to contain one line
    int x[20][100000]; // int values saved in a matrix
    int line = 0; // counter for lines
    int j = 0; // counter for lines (excluding headers)
    FILE *fp;
    char fname[] = "/Users/basho/data_TS-20.csv";

    fp = fopen(fname, "r");
    if(fp == NULL) {
        printf("%s file not open!\n", fname);
        return -1;
    }

    // read one line at a time using fgets
    while (fgets(buffer, sizeof buffer, fp) != NULL) {
        if (line > 1) // we first skip the two first lines of the file, some empty line and the header.
        {
            printf("line %d\n",line);
            sscanf(buffer, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d, ",
                   &x[0][j], &x[1][j], &x[2][j], &x[3][j], &x[4][j], &x[5][j], &x[6][j], &x[7][j], &x[8][j], &x[9][j],
                   &x[10][j], &x[11][j], &x[12][j], &x[13][j],&x[14][j], &x[15][j], &x[16][j], &x[17][j], &x[18][j],
                   &x[19][j], &x[20][j]);
            for(int i = 0; i<20; i++){
                printf("%d ",x[i][j]);
            }
            printf("%d\n",x[20][j]);
            j = j+ 1;
            //}
        }
        line =line + 1;

    }

    fclose(fp);

    return 0;
}

Upvotes: 0

Views: 127

Answers (1)

chux
chux

Reputation: 153468

Review int x[20][100000]; and &x[20][j] --> off by 1.

// int x[20][100000];
int x[21][100000];

Also

j = j+ 1;
if (j >= 100000) break; 

Upvotes: 3

Related Questions