Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How much was read during istream::read ?

Reply
Thread Tools

How much was read during istream::read ?

 
 
Steve
Guest
Posts: n/a
 
      05-13-2004
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.

 
Reply With Quote
 
 
 
 
Buster
Guest
Posts: n/a
 
      05-13-2004
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.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-13-2004

"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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-13-2004

"Buster" <(E-Mail Removed)> wrote in message
news:c7v3qk$k3e$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Buster
Guest
Posts: n/a
 
      05-13-2004
John Harrison wrote:
> "Buster" <(E-Mail Removed)> wrote
>>Use istream::readsome.
>>

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


Yes. Thanks a lot; apologies to OP.

--
Regards,
Buster.
 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      05-13-2004
On 13/5/04 7:18 am, in article http://www.velocityreviews.com/forums/(E-Mail Removed), "John Harrison"
<(E-Mail Removed)> 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.

 
Reply With Quote
 
Steve
Guest
Posts: n/a
 
      05-13-2004
On 13/5/04 7:29 am, in article c7v4kk$la0$(E-Mail Removed), "Buster"
<(E-Mail Removed)> wrote:

> John Harrison wrote:
>> "Buster" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
DVD Soon or much much later? anthony DVD Video 10 07-08-2005 07:13 PM
Simulation questions...how much is too much? =?Utf-8?B?VGlwcHk=?= Microsoft Certification 0 04-16-2005 04:47 AM
CPU Heat--how much is too much? PowerPost2000 Computer Support 4 12-22-2003 12:40 AM
paranoia... much too much adcl Computer Support 14 11-08-2003 05:18 PM



Advertisments