【问题标题】:proc_open not writing to STDIN on simple C programproc_open 没有在简单的 C 程序上写入 STDIN
【发布时间】:2013-12-06 11:08:25
【问题描述】:

下面是使用 proc_open 运行程序(最后是 test.exe-code)的 php 脚本。 它只是挂在浏览器中。 测试程序没有将这两个文件写入目录。但是,如果我取出 stream_get_contents 行并在写入 STDIN 后添加它,它会回显 STDOUT。这两个文件都没有写,我不明白为什么。有什么建议么? 编辑:echo fgetc($pipes[1]); 不会阻止...嗯

<?
test(234);

function test($number) {
  $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","whatever.log", "a") // stderr is a file to write to
  );

    $process = proc_open("cd \"C:/\" && test.exe", $descriptorspec, $pipes);

if (is_resource($process)) {
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);

fwrite($pipes[0],$number);
fclose($pipes[0]);

    return !$return_value;
  }
}
?>

C 程序(在 cmdline 中按预期工作):

#include<stdio.h>

void main()
{
int i;
FILE *ofp;

printf("Please enter an integer number");
fflush ( stdout );
scanf("%d", &i);

ofp = fopen("test.txt", "w");
fprintf(ofp, "%d", i);
fclose(ofp);
}

【问题讨论】:

    标签: php c


    【解决方案1】:

    想通了: 您似乎需要为 windows 上的 freads 指定缓冲区:

    echo fread($pipes[1], 1024);
    

    【讨论】:

      猜你喜欢
      • 2011-12-25
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      相关资源
      最近更新 更多