0

我正在尝试使用 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 输出

4

1 回答 1

0

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

利用

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

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

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

于 2013-09-23T18:08:33.887 回答