如何从C中的stdout读取 [英] how to read from stdout in C
本文介绍了如何从C中的stdout读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要编写一个 C 程序 (myprogram
) 来检查其他程序的输出.它应该基本上是这样工作的:
I need to write a C program (myprogram
) which checks output of other programs. It should basically work like this:
./otherprogram | ./myprogram
但我找不到如何从 stdout
(或管道)中逐行读取,然后将所有这些写入 stdout
.
But I could not find how to read line-by-line from stdout
(or the pipe), and then write all this to stdout
.
推荐答案
使用以下方法创建可执行文件:
Create an executable using:
#include <stdio.h>
int main()
{
char line[BUFSIZ];
while ( fgets(line, BUFSIZ, stdin) != NULL )
{
// Do something with the line of text
}
}
然后您可以将任何程序的输出通过管道传输到它,逐行读取内容,对每一行文本进行处理.
Then you can pipe the output of any program to it, read the contents line by line, do something with each line of text.
这篇关于如何从C中的stdout读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文