473,473 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3425
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gunnar | last post by:
Hello, I've just written a CPP program that reads integers from a binary file, and used this code while (my_ifstram.read( (char* ) &number, sizeof(int)) { // do something with number } My...
1
by: Brian | last post by:
I'm having intermittent trouble with a call to the Read method of the HttpWebResponse object. I get an ArumentOutOfRangeException claiming that deep down inside of the Read method, the count...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
3
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
21
by: Peter Larsen [] | last post by:
Hi, I have a problem using System.Runtime.InteropServices.ComTypes.IStream. Sample using the Read() method: if (IStreamObject != null) { IntPtr readBytes = IntPtr.Zero;...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
6
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have...
5
by: brad | last post by:
How would I determine the number of bytes that is.read actually read? // allocate memory char * buffer; while (!is.eof()) { buffer = new char ;
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.