473,729 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ 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 3473
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::readso me.
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.7 183B%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.po l.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::readso me.


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::readso me.

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.7 183B%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**********@ne ws6.svr.pol.co. uk, "Buster"
<no***@nowhere. com> wrote:
John Harrison wrote:
"Buster" <no***@nowhere. com> wrote
Use istream::readso me.

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
9002
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 question is now, where can I find a manual that describes what the read method does with the ifstream object? I'm sitting here with my Linux/Debian machine, but I have not found any
1
6422
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 parameter of the BlockCopy method is showing up as a negative number. Don't know where the negative number is coming from since I've passed the value of 1024 to the Read and Write methods. I've even tested the value of buffer.Length and size at the...
21
2523
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 constructors class MyClass { Foo foo; // foo and bar require default constructors Bar bar; public:
12
11662
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 currently have as a test for using std::ifstream::read(). Is there anything wrong with the way I'm getting the file? #include <vector> #include <iomanip> #include <fstream> #include <iostream>
3
3379
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 file aren't really useful to a user, I use the IStream::Read function together with a StreamWriter to extract single files from my compound document. When I first tested these functions everything seemed to work fine (and basically, it does),...
21
6914
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; IStreamObject.Read(buffer, size, readBytes);
6
5715
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 * vector<string>. Now, use istringstream to read read each line * from the vector a word at a time.
6
342
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 following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
5
3576
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
8917
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9281
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6722
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.