我需要编写一个C程序来检查其他程序的输出.它应该基本上像这样工作:
./otherprogram|./myprogram
但我找不到如何从stdout(或管道)逐行读取.然后将所有这些写入stdout.
一个程序的标准输出成为下一个程序的标准输入.刚从stdin读取,你会没事的.
shell运行myprogram时会为你连接一切.
BTW,这是负责的bash代码:http: //git.savannah.gnu.org/cgit/bash.git/tree/execute_cmd.c
寻找execute_pipeline.没有代码不容易遵循,但它完全解释了它.
使用以下命令创建可执行文件:
#include <stdio.h>
int main()
{
char line[BUFSIZ];
while ( fgets(line, BUFSIZ, stdin) != NULL )
{
// Do something with the line of text
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将任何程序的输出通过管道传输给它,逐行读取内容,对每一行文本执行某些操作。
| 归档时间: |
|
| 查看次数: |
7078 次 |
| 最近记录: |