I would like to convert a boost::asio::streambuf into a std::string.
How do I do that easily?
Asked
Active
Viewed 2.3k times
7

hookenz
- 36,432
- 45
- 177
- 286
3 Answers
10
I use this aproach:
boost::asio::streambuf stream_buf;
...
std::string s( (std::istreambuf_iterator<char>(&stream_buf)), std::istreambuf_iterator<char>() );
you can read whole data from other kind of streams, f.e., ifstream.

IronFil
- 133
- 1
- 7
2
Did not try this, but if I read the docs correctly, this class inherits from std::streambuf
, in which case you can do this:
std::istream buffer( my_asio_streambuf_ptr );
std::stringstream string_buffer;
string_buffer >> buffer.rd_buf();
There are many ways to do this, and each has it's pros and cons. If you could explain you problem in more detail, we can offer more specific help.

Zitrax
- 19,036
- 20
- 88
- 110

Björn Pollex
- 75,346
- 28
- 201
- 283
-1
Something like this is probably what you're after:
boost::asio::streambuf myBuffer;
std::string myString;
// Convert streambuf to std::string
std::istream(&myBuffer) >> myString;

James M.
- 1
- 1
-
7this will only grab the first set of characters. It will stop when it reads in whitespace. – Trevor Hickey Apr 22 '13 at 06:37