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