Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

memorystream - stringstream, string, others?

i am reading in a binary file via the usual c++/STL/iostream syntax. i am copying the whole content into an dynamically allocated char array and this works fine so far.

but since i want to serve parts of the content as lines to another part of the program, i think it would be better/easier to stick to streams because i don't want to hack around with cstring functions and pointers.

my question now is, how can i store the read in memory. in a stringstream? or in a string? which fits better? are there any advantages or disadvantages of one over the other?

thanks in advance!

like image 728
didito Avatar asked Oct 13 '09 18:10

didito


1 Answers

If you want to read from it as a stream, you might as well read directly from the file to the stringstream:

std::stringstream data;
data << input_file.rdbuf();

That reads the entire contents of 'input_file' into 'data'. You can read the data from there like you would any other stream.

like image 176
Jerry Coffin Avatar answered Sep 23 '22 22:09

Jerry Coffin