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!

13.10.2009 18:44:10
ok, copying is easy after reading this stackoverflow.com/questions/132358/… this (if it is true) also states that stringstreams internal buffer is a string (hence the name i guess). how can i reserve bytes for this buffer - i did not see any note on that in the ctor or anything about a reserve function ...
didito 13.10.2009 18:53:43
1 ОТВЕТ
РЕШЕНИЕ

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.

4
13.10.2009 18:51:07
thanks, already got that. the question is now is it wise to store a stringstream as a kind of memory/char buffer in my class? or should i use a string?
didito 13.10.2009 19:23:21
and what about std::stringbuf?
didito 13.10.2009 19:25:32
Lacking a specific reason to do otherwise, I'd just store the stringstream.
Jerry Coffin 13.10.2009 19:43:19