C ++ Piped数据以块的形式打印出来

时间:2018-03-12 00:01:50

标签: c++ linux pipe cout

我正在尝试编写一个程序,该程序将另一个程序的stdout分割成行并打印出来。

这个其他程序不断输出数据(每秒约800个字符)不间断。

我已将它分叉并使用dup2将其stdout发送到父进程的管道。

问题是父进程读取数据但它只能以每隔6秒大约150行的块打印出来。

所以它打印数据,然后打印6秒,然后更多数据,然后没有......一遍又一遍

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

int main()
{
    int pipe1[2];
    char readbuf[5];
    string line = ">>";

    if (pipe(pipe1) != 0){                  //Setting up the pipe
        cerr << "Pipe failed\n"; exit(1);
    }
    pid_t pid = fork();                     //Setting up the fork

    if (pid < 0){
        cerr << "Fork failed\n" ;exit(-1);
    }
    else if (pid == 0){
                                            //Child Process
        dup2(pipe1[1], STDOUT_FILENO);      //dup2()ing stdout to pipe 
        close(pipe1[1]);                    //closing other pipe connections
        close(pipe1[0]);

        execl("program_folder/program", "program_folder/program", NULL); //running the program
    }
    else{
                                //Parent
        close(pipe1[1]);        //closing write end of pipe

        for (;;){

                do{
                    if (read(pipe1[0], readbuf, 1) < 0){
                        cout << "read error"<<endl;
                        return 1;
                    }                           //Reads 1 character at a time,
                    line.push_back(readbuf[0]); //pushing them onto a string,    
                }while(readbuf[0] != '\n');     //until the new line character is reached

                cout<< line;                    //Prints the line of data
                line= ">>";
        }
        close(pipe1[0]);
    }
    return 0;
}

输出如下所示:(偏航,俯仰,滚动值)

>>ypr    63.71  -68.52   19.24
>>ypr    63.75  -68.52   19.24
>>ypr    63.78  -68.52   19.24
>>ypr    63.81  -68.52   19.24
>>ypr    63.85  -68.52   19.24
>>ypr    63.89  -68.52   19.23
>>ypr    63.93  -68.52   19.24
>>ypr    63.97  -68.52   19.24
>>ypr    64.00  -68.52   19.24

将来我想通过套接字将它们发送到另一台计算机并实时绘制它们,例如gnuplot。我知道使用python会更容易,但我只想用C语言尝试。

我尝试过使用std :: endl;和std :: cout.flush();但没有运气。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

当您使用管道时,数据流中的每行输出不再有。这是一个普遍的UNIX问题。 stdout检查输出流是否为tty(通过调用isatty(3)),因此它会在输出中找到的每个\n处刷新输出,或者当BUFSIZ字符填满缓冲区时。由于程序中有pipe(2) d,因此不再有行缓冲,因此缓冲区在块块中刷新。您可以在需要刷新时强制执行,方法是在适当的位置(C语言)插入fflush(stdout);的呼叫。或者正如评论中所指出的那样(感谢@spectras),使用以下行(仅适用于C ++):

cout <<  "your line here" << std::flush;

(您必须编辑管道编写器,因为您无法控制编写器缓冲区如何从读取器进程操作。)