Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a boost::asio::streambuf into a std::string? [duplicate]

Tags:

c++

boost-asio

I would like to convert a boost::asio::streambuf into a std::string.
How do I do that easily?

like image 379
hookenz Avatar asked Dec 14 '09 08:12

hookenz


2 Answers

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.

like image 107
IronFil Avatar answered Sep 18 '22 14:09

IronFil


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.

like image 31
Björn Pollex Avatar answered Sep 20 '22 14:09

Björn Pollex