如何捕获过程输出使用C? [英] How to capture process output in C?

查看:153
本文介绍了如何捕获过程输出使用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有PHP在空调系统的模拟?

Is there any analog of PHP's system in C?

人系统说,该命令是系统返回状态,但我需要的输出(像PHP )。

man system says, that system return status of the command, but I need the output (like in PHP).

当然,我可以利用这个管道,但没有任何标准的方式?

Of course, I can use pipes for this, but is there any standard way?

推荐答案

您可以使用的 的popen 及相关功能:

You can make use of popen and related function as:

// command to be run.
char *cmd = "date"; 

// open pipe stream.
FILE *fp = popen(cmd,"r");
int ch; 

// error checking.
if(!fp) {
        fprintf(stderr,"Error popen with %s\n",cmd);
        exit(1);
}   

// read from the process and print.
while((ch = fgetc(fp)) != EOF) {
        putchar(ch);
}

// close the stream.
pclose(fp);

Ideone链接

这篇关于如何捕获过程输出使用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆