Is there anyway I can transfer data from an fstream
(a file) to a stringstream
(a stream in the memory)?
Currently, I\'m using a buffer, but th
The only way using the C++ standard library is to use a ostrstream
instead of stringstream
.
You can construct a ostrstream
object with your own char buffer, and it will take ownership of the buffer then (so no more copying is needed).
Note however, that the strstream
header is deprecated (though its still part of C++03, and most likely, it will always be available on most standard library implementations), and you will get into big troubles if you forget to null-terminate the data supplied to the ostrstream.This also applies to the stream operators, e.g: ostrstreamobject << some_data << std::ends;
(std::ends
nullterminates the data).