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"); 試試看
------解決方案--------------------
啥意思?
他讓你將逐次輸出的資料群組裝成串後一次性輸出