csv2 icon indicating copy to clipboard operation
csv2 copied to clipboard

Reading from an std::istream

Open karimhm opened this issue 5 years ago • 5 comments

Is it possible to parse CSV from an std::istream?

karimhm avatar May 11 '20 03:05 karimhm

Yes. You'll have to read the stream into a string like:

std::string s(std::istreambuf_iterator<char>(stream), {});

Then, you can use:

Reader<...> reader;
reader.parse(s);

p-ranav avatar May 11 '20 12:05 p-ranav

But std::istreambuf_iterator<char> will read the entire stream before passing it to the reader, this will consume a lot of memory because I'm working on an app that process large CSV files and put them into a database.

karimhm avatar May 11 '20 17:05 karimhm

I see. So you have an ifstream and would like to parse the file contents. If it is a file, currently csv2 supports memory-mapping it for parsing instead of reading from stream because it is significantly faster to do so. If you're looking to not consume memory, I'd recommend you go down that road anyway. csv2 does not allocate during the file parsing at all. Reading from istream is quite a bit slower - primarily because istream::read copies the block of data being read into the input buffer.

p-ranav avatar May 11 '20 17:05 p-ranav

The CSV files resides inside a .zip archive, so I have an std::istream derived class that reads from within the archive. Extracting the archive is not practical because the size would be enormous

karimhm avatar May 11 '20 17:05 karimhm

I see. Okay I'll add a method that can read from std::istream. Thanks for the explanation.

p-ranav avatar May 11 '20 18:05 p-ranav