469,632 Members | 1,750 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

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.

Jul 22 '05 #1
6 3050
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.
Jul 22 '05 #2

"Steve" <po********@127.0.0.1> wrote in message
news:BCC8CFEE.7183B%po********@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
Jul 22 '05 #3

"Buster" <no***@nowhere.com> wrote in message
news:c7**********@news5.svr.pol.co.uk...
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
Jul 22 '05 #4
John Harrison wrote:
"Buster" <no***@nowhere.com> wrote
Use istream::readsome.

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


Yes. Thanks a lot; apologies to OP.

--
Regards,
Buster.
Jul 22 '05 #5
On 13/5/04 7:18 am, in article 2g************@uni-berlin.de, "John Harrison"
<jo*************@hotmail.com> wrote:

"Steve" <po********@127.0.0.1> wrote in message
news:BCC8CFEE.7183B%po********@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.

Jul 22 '05 #6
On 13/5/04 7:29 am, in article c7**********@news6.svr.pol.co.uk, "Buster"
<no***@nowhere.com> wrote:
John Harrison wrote:
"Buster" <no***@nowhere.com> 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.

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Gunnar | last post: by
12 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by KWienhold | last post: by
21 posts views Thread by Peter Larsen [] | last post: by
6 posts views Thread by arnuld | last post: by
6 posts views Thread by zl2k | last post: by
5 posts views Thread by brad | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.