How to convert boost :: asio :: streambuf to std :: string?

I would like to convert boost :: asio :: streambuf to std :: string.
How can I do this easily?

+3
source share
3 answers

I am using this aproach:

boost::asio::streambuf stream_buf;
...
std::string s( (std::istreambuf_iterator<char>(&stream_buf)), std::istreambuf_iterator<char>() );

you can read integer data from other streams, i.e. ifstream.

+5
source

Did not try this, but if I read the documents correctly, this class inherits from std::streambuf, in which case you can do this this:

std::istream buffer( my_asio_streambuf_ptr );
std::stringstream string_buffer;

buffer >> string_buffer.rd_buf();

There are many ways to do this, and each has its pros and cons. If you can explain the problem in more detail, we can offer more specific help.

+1
source

- , , , :

boost::asio::streambuf myBuffer;
std::string myString;  

// Convert streambuf to std::string  
std::istream(&myBuffer) >> myString;  
0

Source: https://habr.com/ru/post/1725595/


All Articles