473,597 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

istream read not reading everything

Hi,

I'm having trouble using istream to read in a file in its entirety on
UNIX. I've written a dummy program that essencially reads in a file
from stdin and writes it out to a file. When I cat a binary file
through a unix pipe to the program (cat file | prog) everything works
fine. However, when I feed data from a program, one that connects to an
open socket so that multiple files can be processed, across a unix pipe
(prog1 | prog) the end of each file passed in gets buffered until more
data is passed through. The buffering seems to be on my end as the
sending program uses a unix write to send bytes to stdout and I checked
to make sure it wrote out the entire file. When I change my program to
use a unix read (such as read (STDIN_FILENO, buf, size)), I do not see
the problem. So my thoughts were that it may be the istream that's
causing the buffering of the missing data. I was wondering if anyone
could confirm this and had any suggestion on how I could get around it
besides just using the unix read. I've include a copy of my program
below. I have the read set to nonblocking because that's what I need to
do in the real program. I'm running this on a Solaris 5.8 OS.

Thanks in advance for your help,

-joe

int isready (int fd)
{
int retval = 0;
pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI;
fds[0].revents = 0;

if ((retval = poll(fds, 1, 100)) > 0)
return 1;
/* if ((fds[0].revents & POLLIN) || (fds[0].revents & POLLHUP))
return 1;
*/
return 0;

}

int main (int argc, char *argv[])
{
int timecount = 0;
istream *infile = new istream (cin.rdbuf ());
char buf[250];
int count = 0;
char filename[] = "out1.file" ;
ofstream *outfile = new ofstream (filename);

while (1)
{
if (isready (STDIN_FILENO)) {

infile->read(buf, 100);
outfile->write (buf, infile->gcount () );
count+= infile->gcount ();
if (infile->gcount () != 100){
cerr << "Pipe Timed out " << count << endl;
timecount++;
sleep (1);
}
else
timecount = 0;
}
else{
timecount++;
cerr << "Time is " << timecount << " " << count << endl;
sleep (1);
}

if (timecount > 20){
cerr << "Timed out " << count << endl;
outfile->close ();
delete outfile;
delete infile;

exit (0);
}
}
}

Jul 23 '05 #1
3 5692
> I'm having trouble using istream to read in a file in its entirety on
UNIX.

int main (int argc, char *argv[])
{
int timecount = 0;
istream *infile = new istream (cin.rdbuf ());
char buf[250];
int count = 0;
char filename[] = "out1.file" ;
ofstream *outfile = new ofstream (filename);
This isn't Java. You would have much better success with:

.. istream &infile = cin;
.. ofstream outfile(filenam e);

and change all your -> to . and don't 'delete' them.
while (1)
{
if (isready (STDIN_FILENO)) {

infile->read(buf, 100);


This will block until you either receive 100 characters,
or an EOF. For non-blocking I/O I suggest you stick to
your POSIX system calls. If you want stream formatting
functions, you could read your file into a stringstream.

Jul 23 '05 #2
> I'm having trouble using istream to read in a file in its entirety on
UNIX.

int main (int argc, char *argv[])
{
int timecount = 0;
istream *infile = new istream (cin.rdbuf ());
char buf[250];
int count = 0;
char filename[] = "out1.file" ;
ofstream *outfile = new ofstream (filename);
This isn't Java. You would have much better success with:

.. istream &infile = cin;
.. ofstream outfile(filenam e);

and change all your -> to . and don't 'delete' them.
while (1)
{
if (isready (STDIN_FILENO)) {

infile->read(buf, 100);


This will block until you either receive 100 characters,
or an EOF. For non-blocking I/O I suggest you stick to
your POSIX system calls. If you want stream formatting
functions, you could read your file into a stringstream.

Jul 23 '05 #3
Thanks for the advice. I think I am going to stick with the POSIX
calls.

-joe

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3450
by: Steve | last post by:
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
6
2997
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a function to count lines in a large file that start with a particular pattern. The file can contain large amounts of non-text crap, which may make some lines very long (so using getline() with a std::string isn't feasable). So I dug around looking...
5
2362
by: Jim Langston | last post by:
In one of my files I am outputting the value of a char into a human readable file. That is, char a = 123; std::ofstream CharFile( ("Players\\" + Name + ".char").c_str()); if ( CharFile.is_open() ) CharFile << (int) a; So the file has the character a stored as "123". That was the easy part, now comes the fun of reading it back into the char.
13
10975
by: Gianni Mariani | last post by:
What I would like to do is read bytes from a stream, any number and any time. I would like it to wait until there are any bytes to read. I want the exact same functionality as cstdio's "fread" but in a std::istream. It appeared at first that "readsome" would do exactly what I wanted but it appears not to work very well at all (at least with gcc!). When reading from a named pipe on gcc, it returns immediately - no errors, just...
3
3373
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
6899
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);
4
4467
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, I am developing website application in asp.net , visual C# and atl com. I am using atl com component in visual C# application. One of the function of com component interface returns IStream interface. I want to read data from that IStream interface. I am new to visual c#. I have written some code. But, it is returning whole data. It is returning some null characters.
2
7837
by: david.crow | last post by:
Given the following input file: bob 1 2 3 4 5 mary 2 3 4 5 6 7 susan 3 4 5 6 7 8 9 This code snippet does not read it correctly: class Student {
0
2731
by: admb600 | last post by:
I wouldn't normally beg but my dissertation is due in, in a couple of weeks and I am in way over my head here.. Fixing this issue will make or break the project and my degree.. The goal is to extract the raw html from Internet Explorer using a BHO. When accessing the HTML via the mshtml.HTMLDocument (DOM) object you get a version of the HTML that is different from what you see when you "right click --> view source". IE changes the code for...
0
8381
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
8035
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,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5431
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
3886
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
3927
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2404
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
1
1494
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1238
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.