istream read not reading everything

Discussion in 'C++' started by lpe540@yahoo.com, Feb 1, 2005.

  1. Guest

    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);
    }
    }
    }
    , Feb 1, 2005
    #1
    1. Advertising

  2. Old Wolf Guest

    > 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.
    Old Wolf, Feb 1, 2005
    #2
    1. Advertising

  3. Old Wolf Guest

    > 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.
    Old Wolf, Feb 2, 2005
    #3
  4. Guest

    Thanks for the advice. I think I am going to stick with the POSIX
    calls.

    -joe
    , Feb 2, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve
    Replies:
    6
    Views:
    3,678
    Steve
    May 13, 2004
  2. Nomak
    Replies:
    4
    Views:
    600
    Ali Cehreli
    Aug 16, 2004
  3. Eli  Luong

    ifstream not reading everything

    Eli Luong, Jun 6, 2006, in forum: C++
    Replies:
    8
    Views:
    515
    Marcus Kwok
    Jun 6, 2006
  4. Replies:
    2
    Views:
    481
  5. xmllmx
    Replies:
    5
    Views:
    588
    Jorgen Grahn
    Jun 15, 2010
Loading...

Share This Page