473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 5675
> 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(filename);

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(filename);

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
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...
6
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...
5
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()...
13
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"...
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;...
4
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...
2
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
0
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...
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,...

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.