![](/img/trans.png)
[英]How to redirect the output of a system call to inside the program in C/C++?
[英]'cut' output redirect to c++ program
我正在尝试使用cut
更改命令ping
的 output 以获得一些必要的信息,然后将其重定向到 c++ 程序与 bash 管道。
ping google.com | cut -d " " --fields 8 | ./a.out
C++ 程序做这样的事情:
int main(){
string str;
cin >> str;
cout << "str:" << str << endl;
}
我建议,这样的命令可能有效,但它没有,执行上述命令后没有任何 output。 但它在不使用cut
的情况下工作得很好。 如果我只使用
ping google.com | ./a.out
我的程序执行 output。 所以我认为问题是我的 c++ 程序无法从cut
读取字符串。
怎么可能修复它?
是的。 ping
实用程序是行缓冲的(它在输出每一行后执行fflush(stdout)
)。
您可以使用 GNU coreutils 中的stdbuf
命令将整个管道设置为使用行缓冲:
stdbuf -oL -eL bash -c 'ping google.com | cut -d " " --fields 8 | ./a.out'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.