By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

istream read not reading everything

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
> 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

P: n/a
> 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

P: n/a
Thanks for the advice. I think I am going to stick with the POSIX
calls.

-joe

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.