14

I was making an online judge and ran into bit of a problem

What I've done till now?

So my php code takes user code and gives it to a function Compile() which compiles the code and reports back whether the compilation has been successful or not. This part of the code works pretty well.

Now the things left are running the code and evaluation

My problem

My problem is how to handle stdin inputs for user. User generally takes input from stdin using scanf, BufferedReader etc and these values are generally taken using keyboard. Now supposing that i have written those values in a file. How will i feed them these value.

My Attempts

Well I was searching for various ways and i came across this

fopen('php://stdin', 'w') 

If i believe that this works like a file than wouldn't it cause problem if multiple users use stdin at the sametime.

4

2 回答 2

5

如果你有编译好的代码并且知道它的语言,那就简单一点。

您可以使用exec函数来执行代码,并且可以将其用作命令,如下所示(对于 ac 程序,并经过测试):

$output = array();
exec("./main < sample_input.txt", $output);

如果您现在检查 $output var,它在数组中为输出的每一行都有一个条目。

希望这可以帮助。

于 2013-11-04T17:34:49.380 回答
2

确切地说, exec 是您在 PHP 中所需要的。

但是,只要您不仅需要运行,还需要评估,您应该构建一些 shell 脚本来运行、提供、接收输出并检查输出是否正确。请记住正确沙箱该脚本,因为它是安全威胁。

顺便说一句,执行。还提供第三个参数,即返回值。只要您使用自己的返回码构建自己的 shell 脚本,它就非常有用。

exec ('./script.sh',$output,$exit_code)

请记住,php 脚本必须具有文件和目录的权限。

于 2013-11-09T00:37:06.993 回答