如何从boost :: asio :: streambuf获取二进制数据

时间:2011-02-04 09:51:53

标签: boost boost-asio

如何将数据从streambuf刷新到文件? 我试过了

read(*socket_, streamBuf, boost::asio::transfer_at_least(694784))
std::istream is(&streamBuf);
std::ofstream outfile;
outfile.open ("test.exe");
is >> outfile;
outfile.close()

但这不起作用。 有任何线索怎么做?

1 个答案:

答案 0 :(得分:4)

您可以尝试buffer_cast。这是一个例子:

boost::asio::streambuf buf;
size_t bytes = read( *socket_, buf, boost::asio::transfer_at_least(694784) );
buf.commit( bytes );

std::ofstream outfile( "test.exe" );
outfile << boost::asio::buffer_cast<const char*>( buf.data() );