php proc open使用,php中的proc_open+fwrite应用有关问题

在PHP中使用proc_open调用C++编写的exe程序时遇到问题。PHP通过fwrite向进程管道写入参数,但只能写入一次。C++程序期待持续输入。尝试使用fwrite($pipes[0],cmd1 cmd2 cmd3 q);来连续写入多个命令。当在复杂C++程序中执行时,返回错误代码-1073741515。解决方案可能涉及确保正确关闭管道和处理进程间的通信同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

php中的proc_open+fwrite应用问题

我想在php用proc_open调用一个c++写的exe程序,由于里面有要求输入参数所以我用fwrite写入终端,php代码如下

PHP codearray("pipe", "r"), // stdin is a pipe that the child will read from 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", "error-output.txt", "a") // stderr is a file to write to);$process = proc_open("d:\\64test.exe", $descriptorspec, $pipes);if (is_resource($process)) { // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt fwrite($pipes[0], "q"); fclose($pipes[0]); while (!feof($pipes[1])) { $result = fgets($pipes[1], 1024); echo "r:$result

"; } fclose($pipes[1]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close ($process); echo "command returned $return_value

";}?>

c++代码如下

C/C++ code#include using namespace std;int main(int argc,char *argv[]){ char buf[1024]={0}; /*fgets(buf,sizeof(buf),stdin);*/ while(buf[0]!='q') { std::cin>>buf; std::cout<

上面的c++是简化的代码

(1)问题是我的c++中代码是个while循环要求持续的输入

问php中应该怎么写,现在的的php中只能fwrite($pipes[0], "q");,其它的就不行

(2)上面那段程序到能正常打印

r:q

command returned -3

我在另一个非常复杂的cpp程序去始终打印如下:

r:

command returned -1073741515

后来我在main中把其他的代码都注释掉直接复复制上面简单的c++代码到我的工程中结果还是上面的,这是我最纳闷的地方,有人遇到过么

求教高人,万分感谢

------解决方案--------------------

fwrite($pipes[0], "cmd1\ncmd2\ncmd3\nq"); 试试看

------解决方案--------------------

啥意思?

他让你将逐次输出的数据组装成串后一次性输出

相关文章

相关视频

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值