istream read not reading everything

L

lpe540

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);
}
}
}
 
O

Old Wolf

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.
 
O

Old Wolf

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top