ben-strasser / fast-cpp-csv-parser

fast-cpp-csv-parser
BSD 3-Clause "New" or "Revised" License
2.09k stars 435 forks source link

Count rows without processing them? #125

Closed dominique120 closed 2 years ago

dominique120 commented 2 years ago

Is there a way or a function already implemented to be able to count the rows of the file without processing each line first?

This would be very helpful to be able to handle error thrown by read_row() individually instead of failing the whole file.

dominique120 commented 2 years ago

I ended up finding a solution by doing the following:

    size_t lines = 0;
    io::LineReader ln_reader("file.csv");
    while (char* line = ln_reader.next_line()) {
        ++lines;
    }

...

    for (size_t i = 0; i <= lines; ++i) {
        try {
            in.read_row(...);
        } catch (io::error::too_many_columns::exception) { // or other exceptions
            //handle exception
        }
    }