Reputation: 397
Need a help to implement the following. I have a C program file as follows:
#include <stdio.h>
main()
{
int x;
int args;
printf("Enter an integer: ");
if (( args = scanf("%d", &x)) == 0) {
printf("Error: not an integer\n");
} else {
printf("Read in %d\n", x);
}
if (( args = scanf("%d", &x)) == 0) {
printf("Error: not an integer\n");
} else {
printf("Read in %d\n", x);
}
}
I generated a.out and now I want to call this a.out using exec("a.out", $output). But my problem is that I'm getting how to pass the value of integer when it asks for. I tried using proc_open() but I could not understand its usage. I will appreciate your help if you can give me piece of PHP code which can handle this to pass these two values and finally print the received result.
Best Regards
Upvotes: 2
Views: 566
Reputation: 31834
my guess would be
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w")
);
$process = proc_open('/full/path/to/a.out', $descriptorspec, $pipes);
if (is_resource($process)) {
list ($out, $in) = $pipes;
fwrite($out, "5\n");
fwrite($out, "7\n");
echo stream_get_contents($in);
}
Upvotes: 3