我是php的新手,我正在尝试编写一个PHP脚本来编译和执行C源代码。这是我的源代码:
<?php
$cmd = "gcc -g test.c -o test";
$descriptors = array(
2 => array("pipe","w+")
);
$process = proc_open($cmd,$descriptors,$pipes);
stream_set_blocking($pipes[2],0);
if($error = stream_get_contents($pipes[2]))
{
die($error);
}
fclose($pipes[2]);
proc_close($process);
$cmd = "./test";
$descriptors = array(
0 => array("file","input.txt","r"),
1 => array("file","output.txt","w"),
2 => array("pipe","w+")
);
$process = proc_open($cmd,$descriptors,$pipes);
stream_set_blocking($pipes[2],0);
if($error = stream_get_contents($pipes[2]))
{
die($error);
}
else
{
/*line -> 38 */ stream_set_blocking($pipes[1],1);
/*line -> 40 */ $read = array($pipes[1],$pipes[2]);
$write = NULL;
$except = NULL;
$tv_sec = 0;
$tv_usec = 1000;
/*line -> 46*/ if(false === ($rs = stream_select($read,$write,$except,$tv_sec,$tv_usec)) )
{
die("Error in stream_reader");
}
/*line -> 51*/ fclose($pipes[0]);
/*line -> 52*/ fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}
$out = @fopen("output.txt","r");
$test = @fopen("test.txt","r");
while((($out_buffer = fgets($out)) !== false) && (($test_buffer = fgets($test)) !== false))
{
if($out_buffer !== $test_buffer)
{
die("Error in output.");
}
}
if(!feof($out) || !feof($test))
{
die("Error in output.");
}
?>
这是我的错误:
Undefined offset: 1 in file.php on line 38
stream_set_blocking() expects parameter 1 to be resource, null given in file.php on line 38
Undefined offset: 1 in file.php on line 40
stream_select(): supplied argument is not a valid stream resource in file.php on line 46
stream_select(): supplied argument is not a valid stream resource in file.php on line 46
stream_select(): supplied argument is not a valid stream resource in file.php on line 46
Undefined offset: 0 in file.php on line 51
fclose() expects parameter 1 to be resource, null given in file.php on line 51
Undefined offset: 1 in file.php on line 52
fclose() expects parameter 1 to be resource, null given in file.php on line 52
请有人告诉我我错在哪里,当流是文件时,我们不能不使用stream_set_blocking()吗? 谢谢!