vincentlaucsb / csv-parser

A high-performance, fully-featured CSV parser and serializer for modern C++.
MIT License
901 stars 150 forks source link

Incorrect n_rows() value when using iterators to loop over the data #173

Closed sjoubert closed 4 months ago

sjoubert commented 3 years ago

When using iterator (either old style begin()/end() or through a range-for loop) the value returned by n_rows() is off by 1. This does not happen when using the read_row pattern in a loop.

From what I can tell, the issue seems to come from CSVReader::begin() which gets a data row and construct the first iterator without incrementing the counter. Later ++ calls on the iterator correctly do that through a read_row call.

A possible fix may be to just replace the current approach of begin with a call to read_row instead of explicitly retrieving the data.

sjoubert commented 3 years ago

This may be the cause of #151 depending on the loop method used there.

vincentlaucsb commented 4 months ago

Thanks for your report. Your diagnosis was correct and this issue has been fixed.