Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading from an std::istream #8

Open
karimhm opened this issue May 11, 2020 · 5 comments
Open

Reading from an std::istream #8

karimhm opened this issue May 11, 2020 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@karimhm
Copy link

karimhm commented May 11, 2020

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

@p-ranav
Copy link
Owner

p-ranav commented May 11, 2020

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);

@karimhm
Copy link
Author

karimhm commented May 11, 2020

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.

@p-ranav
Copy link
Owner

p-ranav commented May 11, 2020

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.

@karimhm
Copy link
Author

karimhm commented May 11, 2020

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

@p-ranav
Copy link
Owner

p-ranav commented May 11, 2020

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

@p-ranav p-ranav self-assigned this May 11, 2020
@p-ranav p-ranav added the enhancement New feature or request label May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants