使用 gcc 和 php 编译 C 代码以进行多个输入


Compiling C code using gcc and php for multiple input

我正在尝试使用 gcc 编译器执行 c 代码。 我创建了一个 dynamic_code.c 文件,它是这样的

#include <stdio.h> 
int main()
{
char ch;
char str[100]; 
printf("Enter any character 'n");
scanf("%c", &ch);
printf("Entered character is %c 'n", ch);
printf("Enter any string ( upto 100 character ) 'n");
scanf("%s", &str);
printf("Entered string is %s 'n", str);
} 

现在使用 gcc 编译器,我想在产生输出时执行它。条件是编译时要传递的输入。

使用 php

 $input = 's world';
 exec("gcc dynamic_code.c -o dynamic_code");
 $string="echo '".$input."' | ./dynamic_code";
 $output=exec($string);
 $json = json_encode($output);
 echo $json;

即将出现的输出是

"Entered string is world"

我怎样才能生产

Enter any character
Entered character is S
Enter any string ( upto 100 character )
Entered string is world

谢谢编辑:看到这里...我想要这样的东西http://cfiddle.net/jlmKDA

我认为问题是收集所有输出...仅存储最后一个 printf 输出

$output=exec($string)$output设置为命令结果的最后一行。

exec($command, $output);
print_r($output);

如果存在输出参数,则指定的数组将填充命令的每一行输出。尾随空格(如 ')不包括在此数组中。...

http://php.net/manual/en/function.exec.php