开发者

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

开发者 https://www.devze.com 2022-12-14 10:28 出处:网络
This question already has answers here: Copy a streambuf's contents to a string 开发者_StackOverflow(11 answers)
This question already has answers here: Copy a streambuf's contents to a string 开发者_StackOverflow (11 answers) Closed 7 years ago.

I would like to convert a boost::asio::streambuf into a std::string.

How do I do that easily?


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.


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.


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;  
0

精彩评论

暂无评论...
验证码 换一张
取 消