Boost asio - async reading established number of chars from stdin -
i wanna write boost::asio app reading stdin boost::asio::streambuf. anyway function works on streambuf made stdin_fileno boost::asio::async_read_until. other ones throws errors. there possibility read 100 first character stdin boost asio function?
in principle works
#include <boost/asio.hpp> #include <boost/asio/posix/stream_descriptor.hpp> using namespace boost::asio; using boost::system::error_code; #include <iostream> int main() { io_service svc; posix::stream_descriptor in(svc, stdin_fileno); char buf[100]; async_read(in, buffer(buf,sizeof(buf)), [&](error_code ec, size_t br) { std::cout << std::string(buf, br) << std::flush; if (ec) std::cerr << ec.message(); }); svc.run(); }
when used as
cat input.txt | ./test | wc -c
will output 100
expected (and echo input). can use live terminal input:
./test | wc -c
when input shorter 100 bytes, ec.message()
"end of file" printed too.
what does not work on linux is:
./test < input.txt
you receive:
terminate called after throwing instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >' what(): assign: operation not permitted
this because regular files not supported async operations: strange exception throw - assign: operation not permitted
Comments
Post a Comment