简体   繁体   English

将std :: string转换为boost :: asio :: streambuf

[英]Convert std::string to boost::asio::streambuf

How do I convert the std::string to boost::asio::streambuf ? 如何将std::string转换为boost::asio::streambuf Any lead is appreciated. 任何线索表示赞赏。

Initially I used boost::asio::read_until(pNetworkConnector->getSocket(), response, "\\r\\n"); 最初,我使用boost::asio::read_until(pNetworkConnector->getSocket(), response, "\\r\\n"); which initialized boost::asio::streambuf response and I converted it to underlying stream ie 初始化boost::asio::streambuf response然后将其转换为基础流,即

boost::asio::streambuf* responsePointer = &response;
iResponseStream.rdbuf(responsePointer);

But now I directly get a std::string using curl like this 但是现在我像这样使用curl直接得到一个std::string

static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

and used it as 并用作

curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &readBuffer);

How can I convert std::string to boost::asio::streambuf or std::streambuf ? 如何将std::string转换为boost::asio::streambufstd::streambuf

The following code shows how to convert between std::string and boost::asio::streambuf , see it online at ideone : 以下代码显示了如何在std::stringboost::asio::streambuf之间进行转换,可在ideone在线查看

#include <iostream>
#include <boost/asio/streambuf.hpp>
#include <boost/asio/buffer.hpp>

int main()
{
   /* Convert std::string --> boost::asio::streambuf */
   boost::asio::streambuf sbuf;
   std::iostream os(&sbuf);
   std::string message("Teststring");
   os << message;

   /* Convert boost::asio::streambuf --> std::string */
   std::string str((std::istreambuf_iterator<char>(&sbuf)),
                    std::istreambuf_iterator<char>());

   std::cout << str << std::endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM