php中的proc_open+fwrite应用有关问题

php中文网
发布: 2016-06-13 10:50:40
原创
1620人浏览过
php中的proc_open+fwrite应用问题
我想在php用proc_open调用一个c++写的exe程序,由于里面有要求输入参数所以我用fwrite写入终端,php代码如下
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?php$descriptorspec = array(   0 => array("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<br>";    }    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<br>";}?>
登录后复制

c++代码如下
C/C++ code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->#include <iostream>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<<buf;    }    return -3;}
登录后复制

上面的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 cmd2 cmd3 q"); 试试看
------解决方案--------------------
啥意思?
他让你将逐次输出的数据组装成串后一次性输出
相关标签:
php
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号