我有这个从标准输入获取输入的小程序 样本.c
#include<stdio.h>
main()
{
int l=0;
scanf("%d",&l);
printf("\n%d",l);
}
当然!编译它:cc sample.c 并得到 a.out 我正在尝试通过 php 来运行它
$runcmd = "./a.out > output.txt";
exec($runcmd,$outp);
print_r($outp);
我的问题是我不知道如何为这个程序提供输入以便 scanf 可以读取它? 请在这里帮助我!
谷歌搜索给出了一些提示,如 proc_open、popen ....但我做不到。
提前致谢。
最佳答案
看看popen http://se1.php.net/popen
它的工作方式有点像 fopen,并且在使用 fwrite 时,不是写入文件,而是写入过程标准输入。
$runcmd = "./a.out > output.txt";
$process_stdin = popen($runcmd, 'w');
fwrite($process_stdin, "text to send to process");
pclose($process_stdin);
关于php - 如何将用户输入从 HTML 表单获取到 C 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17946336/