How much was read during istream::read ?

S

Steve

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.
 
B

Buster

Steve said:
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.
 
J

John Harrison

Steve said:
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
 
S

Steve

gcount()

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


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

john


Ah, OK, got it. Thanks for that.


Steve.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top