1

It's my bad or a bug in boost::asio::streambuf implementation? In down case i create instance of boost::asio::streambuf, then instance of std::istream and write 3 int to streambuf. Then i need read int's from streambuf while it contain a data.

I'm not understand why is.eof() always return false in this code.

I wrote next case:

    void TestCase()
    {
        boost::asio::streambuf sbuf;

        try {
            std::istream is(&sbuf);

            // put three raw ints in streambuf
            for( int i=0; i<3; i++ ) {
                auto sz = boost::asio::buffer_copy( 
                    sbuf.prepare(sizeof(int)),
                    boost::asio::buffer(&i, sizeof(int)) 
                );
                sbuf.commit(sz);
            }

            // read from streambuf while not end-of-stream occured  
            // (not work! why? what wrong?)
            while( !is.eof() ) {
                int t;
                auto sz = is.readsome( reinterpret_cast<char*>(&t), sizeof(int) );

                // this work, but why is.eof() not detect end of stream condition 
                // on next iteration if this condition are commented?
                // ( wrong value of t are correct in this example )
                #if 1
                if( sz < sizeof(int) )
                    break;
                #endif

                std::cout << t << ' ';
            }
        }
        catch(std::exception& e) {
            std::cout << e.what() << std::endl;
        }
    }  
  • 2
    The C++ version of that: [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Feb 05 '14 at 07:31
  • @WhozCraig, thanks, it's a isteam specific. I modify this code to `while( !is.eof() ) { is.read(reinterpret_cast(&t), sizeof(int)); cout << t << ' '; }` and its work as need. But why readsome() not set eofbit? Here http://www.cplusplus.com/reference/istream/istream/readsome/ says what 'Errors are signaled by modifying the internal state flags:' – user3273943 Feb 05 '14 at 08:18
  • 1
    Read the articles linked (probably both of them). EOF isn't "set" until a read *fails*. A perfect read that *reaches* eof will not set it. You need to check your IO operations; not just EOF. As I said. *read the linked question and answer*. [`readsome`](http://en.cppreference.com/w/cpp/io/basic_istream/readsome) only sets eofbit if there is *no* data left *and* you then call it, otherwise it reads what is asked or what is left, returning the size-actually-read as the result. And that site (the one you linked) is *notorious* for inaccurate documentation. Use the one I linked. – WhozCraig Feb 05 '14 at 08:30

0 Answers0