How much was read during istream::read ?

Discussion in 'C++' started by Steve, May 13, 2004.

  1. Steve

    Steve Guest

    Hi,

    I'm trying to convert a file reading loop into one using streams. The BSD OS
    read API returns the number of bytes read, but istream::read returns itself.
    How can I find out the number of bytes actually read?

    What the code fragment should do is read up to 1000 bytes into a buffer, or
    finish early if reading failed. Just your average read loop.

    I have: (this is a simplified version; I know there's no detailed error
    checking!)

    char buffer[1000];
    int bytestoread = 1000;
    int totalbytes = 0;

    int fd = ... ; // a file descriptor

    while( bytestoread )
    {
    int bytesread = read( fd, buffer, bytestoread );
    if( bytesread <= 0 )
    break;
    buffer += bytesread;
    bytestoread -= bytesread;
    totalbytes += bytesread;
    }

    And I want:

    char buffer[1000];
    int bytestoread = 1000;
    int totalbytes = 0;

    std::istream& is( ... ); // an istream

    while( bytestoread )
    {
    is.read( buffer, bytestoread ); // << PROBLEM

    if( bytesread <= 0 )
    break;
    buffer += bytesread;
    bytestoread -= bytesread;
    totalbytes += bytesread;
    }


    The problem is how can I find out how many bytes were really read? And if
    there is, does the mechanism work the same as the OS read API? - ie. Zero to
    indicate end, negative for error, etc.

    (What I am actually trying to do is interface to libxml2 and get some XML to
    be parsed from an istream using xmlCtxtReadIO).


    Thanks for any help.

    --
    Regards,
    Steve.
     
    Steve, May 13, 2004
    #1
    1. Advertising

  2. Steve

    Buster Guest

    Steve wrote:
    > Hi,
    >
    > I'm trying to convert a file reading loop into one using streams. The BSD OS
    > read API returns the number of bytes read, but istream::read returns itself.
    > How can I find out the number of bytes actually read?


    Use istream::readsome.

    > What the code fragment should do is read up to 1000 bytes into a buffer, or
    > finish early if reading failed. Just your average read loop.


    That's a one-liner.

    --
    Regards,
    Buster.
     
    Buster, May 13, 2004
    #2
    1. Advertising

  3. "Steve" <postmaster@127.0.0.1> wrote in message
    news:BCC8CFEE.7183B%postmaster@127.0.0.1...
    > Hi,
    >
    > I'm trying to convert a file reading loop into one using streams. The BSD

    OS
    > read API returns the number of bytes read, but istream::read returns

    itself.
    > How can I find out the number of bytes actually read?


    gcount()

    It's a clunky part of the iostream API I think, but that's the way you do
    it.

    >
    >
    > The problem is how can I find out how many bytes were really read? And if
    > there is, does the mechanism work the same as the OS read API? - ie. Zero

    to
    > indicate end, negative for error, etc.


    gcount() returns the number of bytes read, that is never negative.

    john
     
    John Harrison, May 13, 2004
    #3
  4. "Buster" <> wrote in message
    news:c7v3qk$k3e$...
    > Steve wrote:
    > > Hi,
    > >
    > > I'm trying to convert a file reading loop into one using streams. The

    BSD OS
    > > read API returns the number of bytes read, but istream::read returns

    itself.
    > > How can I find out the number of bytes actually read?

    >
    > Use istream::readsome.
    >


    readsome only reads characters that are immediately available from the
    buffer.

    john
     
    John Harrison, May 13, 2004
    #4
  5. Steve

    Buster Guest

    John Harrison wrote:
    > "Buster" <> wrote
    >>Use istream::readsome.
    >>

    > readsome only reads characters that are immediately available from the
    > buffer.


    Yes. Thanks a lot; apologies to OP.

    --
    Regards,
    Buster.
     
    Buster, May 13, 2004
    #5
  6. Steve

    Steve Guest

    On 13/5/04 7:18 am, in article , "John Harrison"
    <> wrote:

    >
    > "Steve" <postmaster@127.0.0.1> wrote in message
    > news:BCC8CFEE.7183B%postmaster@127.0.0.1...
    >> Hi,
    >>
    >> I'm trying to convert a file reading loop into one using streams. The BSD

    > OS
    >> read API returns the number of bytes read, but istream::read returns

    > itself.
    >> How can I find out the number of bytes actually read?

    >
    > gcount()
    >
    > It's a clunky part of the iostream API I think, but that's the way you do
    > it.
    >
    >>
    >>
    >> The problem is how can I find out how many bytes were really read? And if
    >> there is, does the mechanism work the same as the OS read API? - ie. Zero

    > to
    >> indicate end, negative for error, etc.

    >
    > gcount() returns the number of bytes read, that is never negative.
    >
    > john
    >
    >



    Ah, OK, got it. Thanks for that.


    Steve.
     
    Steve, May 13, 2004
    #6
  7. Steve

    Steve Guest

    On 13/5/04 7:29 am, in article c7v4kk$la0$, "Buster"
    <> wrote:

    > John Harrison wrote:
    >> "Buster" <> wrote
    >>> Use istream::readsome.
    >>>

    >> readsome only reads characters that are immediately available from the
    >> buffer.

    >
    > Yes. Thanks a lot; apologies to OP.



    No problem.
    That's the mistake I made on my first attempt! :)

    Cheers,
    Steve.
     
    Steve, May 13, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    914
  2. Anne-Marte
    Replies:
    3
    Views:
    6,055
    Grant
    Mar 30, 2005
  3. cpp4ever
    Replies:
    3
    Views:
    396
    Francesco
    Sep 8, 2009
  4. Raymond Schanks
    Replies:
    0
    Views:
    575
    Raymond Schanks
    Apr 11, 2010
  5. xmllmx
    Replies:
    5
    Views:
    603
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page