我的应用程序中需要一个整洁的可搜索流缓冲区,并且我已经将boost :: asio与异步套接字和计时器集成在一起。我还想尝试集成一个缓冲区,该缓冲区可用作具有搜索功能的流。我尝试将boost :: asio :: streambuf包装在std :: iostream中,并且tell函数返回-1,并且在搜索时遇到异常。
我用错了还是我使用了错误的缓冲对象?
BufferTest.cpp
#include <boost/asio/streambuf.hpp>
#include <iostream>
/**
* Main when we come in
*/
int main( int argc, char *argv[] )
{
boost::asio::streambuf streambuf;
std::iostream stream( &streambuf );
try
{
stream.exceptions ( std::iostream::failbit | std::iostream::badbit );
stream.write( "hello", 5 );
stream.write( "test", 4 );
std::cout << "Step 1" << std::endl;
char buffer[ 9 ];
stream.read( buffer, 9 );
std::cout << "Step 2" << std::endl;
std::cout << "The worst: " << std::string( buffer, 9 ) << std::endl;
std::cout << "Step 3" << std::endl;
stream.seekg( 0 );
std::cout << "Step 4" << std::endl;
stream.read( buffer, 9 );
std::cout << "The worst: " << std::string( buffer, 9 ) << std::endl;
}
catch( const std::exception &e )
{
std::cerr << "Exception: " << e.what() << std::endl;
}
}
输出:
$ ./bin/buffer_test
Step 1
Step 2
The worst: hellotest
Step 3
Exception: basic_ios::clear: iostream error