ben-strasser / fast-cpp-csv-parser

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

How can get rows number of CSV file #78

Closed cnkjw closed 5 years ago

cnkjw commented 5 years ago

Hello Ben Strasser,

Thanks for you give ours this good project. Now, I have a question, I want to create an array and save all data, which is from the CSV file. In other words, I want to get rows size of CSV file. But I don't find any useful methods(or function) to get this information. Yes, we can use some method as follow get this number,

int num=0;
while(inFile.read_row(a, b)) {
     num++;
 }

but I think it isn't an elegant manner, so how can I do? Thanks!

ben-strasser commented 5 years ago

You can get the current line number using get_file_line. It is technically infeasible to get the number of lines without looking at every byte of the CSV file. Your pattern is therefore not far from being the best there is.

If you want to read the file in one scan, use std::vector and push_back. Reading a file in one scan is probably faster, than scanning the file twice.