捕获c ++ system()命令的缓冲区输出 [英] capture buffer output from a c++ system() command

查看:109
本文介绍了捕获c ++ system()命令的缓冲区输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何捕获在命令行运行的php脚本的输出并将其存储到c ++中的数组?即

How would I capture the output from a php script that I run at the command line and store it to an array in c++? i.e.

system('php api/getSome.php');

推荐答案

当您需要在父进程和子进程之间进行IO时,通常首选popen().它使用基于C的FILE流.它是同样构成了system()的相同底层底层内在函数的包装,即fork + execl,但是与system()不同,popen()打开管道并将stdin/out复制到流中以进行读取和写入.

Traditionally popen() is preferred when you need to IO between your parent and child process. It uses the C based FILE stream. It is a wrapper around the same low level intrinsics that also make up system(), namely fork + execl, but unlike system(), popen() opens a pipe and dups stdin/out to a stream for reading and writing.

FILE *p = popen("ping www.google.com", "r");

然后像文件一样读取它.

Then read from it like a file.

这篇关于捕获c ++ system()命令的缓冲区输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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