Passing interactive arguments for PHP exec() method

577 Views Asked by At

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

1

There are 1 best solutions below

4
On

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);
}