How much was read during istream::read ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    How much was read during istream::read ?

    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.

  • Buster

    #2
    Re: How much was read during istream::read ?

    Steve wrote:[color=blue]
    > 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?[/color]

    Use istream::readso me.
    [color=blue]
    > 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.[/color]

    That's a one-liner.

    --
    Regards,
    Buster.

    Comment

    • John Harrison

      #3
      Re: How much was read during istream::read ?


      "Steve" <postmaster@127 .0.0.1> wrote in message
      news:BCC8CFEE.7 183B%postmaster @127.0.0.1...[color=blue]
      > Hi,
      >
      > I'm trying to convert a file reading loop into one using streams. The BSD[/color]
      OS[color=blue]
      > read API returns the number of bytes read, but istream::read returns[/color]
      itself.[color=blue]
      > How can I find out the number of bytes actually read?[/color]

      gcount()

      It's a clunky part of the iostream API I think, but that's the way you do
      it.
      [color=blue]
      >
      >
      > 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[/color]
      to[color=blue]
      > indicate end, negative for error, etc.[/color]

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

      john


      Comment

      • John Harrison

        #4
        Re: How much was read during istream::read ?


        "Buster" <noone@nowhere. com> wrote in message
        news:c7v3qk$k3e $1@news5.svr.po l.co.uk...[color=blue]
        > Steve wrote:[color=green]
        > > Hi,
        > >
        > > I'm trying to convert a file reading loop into one using streams. The[/color][/color]
        BSD OS[color=blue][color=green]
        > > read API returns the number of bytes read, but istream::read returns[/color][/color]
        itself.[color=blue][color=green]
        > > How can I find out the number of bytes actually read?[/color]
        >
        > Use istream::readso me.
        >[/color]

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

        john


        Comment

        • Buster

          #5
          Re: How much was read during istream::read ?

          John Harrison wrote:[color=blue]
          > "Buster" <noone@nowhere. com> wrote[color=green]
          >>Use istream::readso me.
          >>[/color]
          > readsome only reads characters that are immediately available from the
          > buffer.[/color]

          Yes. Thanks a lot; apologies to OP.

          --
          Regards,
          Buster.

          Comment

          • Steve

            #6
            Re: How much was read during istream::read ?

            On 13/5/04 7:18 am, in article 2ggi9uF2hj5lU1@ uni-berlin.de, "John Harrison"
            <john_andronicu s@hotmail.com> wrote:
            [color=blue]
            >
            > "Steve" <postmaster@127 .0.0.1> wrote in message
            > news:BCC8CFEE.7 183B%postmaster @127.0.0.1...[color=green]
            >> Hi,
            >>
            >> I'm trying to convert a file reading loop into one using streams. The BSD[/color]
            > OS[color=green]
            >> read API returns the number of bytes read, but istream::read returns[/color]
            > itself.[color=green]
            >> How can I find out the number of bytes actually read?[/color]
            >
            > gcount()
            >
            > It's a clunky part of the iostream API I think, but that's the way you do
            > it.
            >[color=green]
            >>
            >>
            >> 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[/color]
            > to[color=green]
            >> indicate end, negative for error, etc.[/color]
            >
            > gcount() returns the number of bytes read, that is never negative.
            >
            > john
            >
            >[/color]


            Ah, OK, got it. Thanks for that.


            Steve.

            Comment

            • Steve

              #7
              Re: How much was read during istream::read ?

              On 13/5/04 7:29 am, in article c7v4kk$la0$1@ne ws6.svr.pol.co. uk, "Buster"
              <noone@nowhere. com> wrote:
              [color=blue]
              > John Harrison wrote:[color=green]
              >> "Buster" <noone@nowhere. com> wrote[color=darkred]
              >>> Use istream::readso me.
              >>>[/color]
              >> readsome only reads characters that are immediately available from the
              >> buffer.[/color]
              >
              > Yes. Thanks a lot; apologies to OP.[/color]


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

              Cheers,
              Steve.

              Comment

              Working...