Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ getline does not seem to recognize newlines in input piped to stdin

Tags:

c++

getline

This code:

$ cat junk.cpp

#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <string>

using namespace std;

int
main(int argc, char **argv)
{
    string line;

    while(getline(cin, line)) 
    {
        cout << line << endl;
    }

    return 0;
}

works fine if I run it, then type "hi" and "there

$ junk hi hi there there

So far so good.

But I have another program:

$ cat junk1.c

#include <stdio.h>

int
main(int argc, char **argv)
{
    int i = 4;

    while(i--)
    {
        printf("abc\n");
        sleep(1);
    }

    return 0;
}

This one outputs 4 lines of "abc\n" and sleeps after each line.

So if I do this:

junk1 | junk

I would expect to see each line of "abc" followed by a 1 second sleep, but instead I see 4 seconds of sleep followed by all 4 "abc" lines at the end.

Apparently getline() is buffering all output from junk1, and only proceding when junk1 terminates.

This is not the behavior I need, because I want to pipe the stdout from a program that runs forever and produces voluminous output, to a program like junk.cpp.

Any idea what is going on here?

Thanks!

like image 733
Sinbad Avatar asked Oct 19 '22 21:10

Sinbad


1 Answers

This is probably caused by stdout in junk1.c being buffered: all writes to it (by printf or any other means) are collected in an in-memory buffer, and only written to stdout when the program exits.

To force stdout to actually write what it has in the buffer, you have to use flush (in your case, you should use it after every printf).

like image 81
lodo Avatar answered Nov 12 '22 20:11

lodo