1

found this lib for csv parsing https://github.com/ben-strasser/fast-cpp-csv-parser and I am trying to get the data into 2d array. I have a problem with useability as the lib is constructed in a specific manner

 bool read_row(ColType1&col1, ColType2&col2, ...);

Going from example presented in github, I came up with:

main() {

double** data = new double*[5];
for (int i = 0; i < 4; ++i) {
    data[i] = new double[5];
}

io::CSVReader<5> in("c:/code/test.txt");
//in.read_header(io::ignore_extra_column, "vendor", "size", "speed");
double a; double b; double c; double d; double e;
int n = 0;
while (in.read_row(a, b, c, d, e)) {
    printf("time elapse: %f\n", c); 

    data[n][0] = a;
    data[n][1] = b;
    data[n][2] = c;
    data[n][3] = d;
    data[n][4] = e;

    n++;

}


for (int i = 0; i < 4; i++){ 
    for (int j = 0; j < 5; j++) {
        printf("data %f\n", data[i][j]);
    }
}       

}

This does the job, goes through 4 lines with 5 columns and as to example I had to present a variable for every column. How to modify the code to take thousands of columns? Is there a better/easier way to process csv into 2d array? My goal is speed, I know exact formating of csv input, num of lines and columns, so no checks needed.

1 Answer 1

1

You say your input CSV files have thousands of columns. The library you have linked does not effectively support such a use case (after all, almost no one has thousand-column CSV files).

A simple and still fast method will be to read one line at a time (std::getline() or fgets()), then split by commas explicitly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.