php中的proc_open+fwrite應用有關問題

來源:互聯網
上載者:User
php中的proc_open+fwrite應用問題
我想在php用proc_open調用一個c++寫的exe程式,由於裡面有要求輸入參數所以我用fwrite寫入終端,php代碼如下
PHP code
 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
"; } 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"); 試試看
------解決方案--------------------
啥意思?
他讓你將逐次輸出的資料群組裝成串後一次性輸出
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.