【发布时间】:2020-12-20 16:19:26
【问题描述】:
我想逐行读取程序的输出,并在每一行之后做一些事情(以“\n”结尾)。以下代码读取 50 个字符的块并打印输出。在换行符到来之前我有什么办法可以阅读吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
FILE* file = popen("some program", "r");
char out[50];
while(fgets(out, sizeof(out), file) != NULL)
{
printf("%s", out);
}
pclose(file);
return 0;
}
【问题讨论】:
标签: c output line stdout popen