views:

38

answers:

1
+2  Q: 

PHP system() args

I have the following code that executes a C++ program and outputs it:

<html>
  <head>
    <title>C++</title>
  </head>
  <body>
    <div><?php 
    system("app.exe", $out);
    echo rtrim($out, "0");
     ?></div>
  </body>
</html>

How can I make it so that you can pass arguments to the c++ program, say like this...

If this was the c++ program

#include <iostream>
#include <string>
int main(){
  string input = getarg();//Not really a function, just one I kinda want to know
  cout << input;
  return 0;
}

Could I do something like this?

<html>
  <head>
    <title>C++</title>
  </head>
  <body>
    <div><?php 
    system("app.exe arg=hello-world", $out);
    echo rtrim($out, "0");
     ?></div>
  </body>
</html>

I don't know a lot of parts to this problem, I can execute the program but I just need to pass arguments.

+1  A: 

You can pass the arguments space separated after the command like system("app.exe hello-world 2 3", $out);

in your c++ program

 int main (int argc, char** argv) {
    // argv[1] will be pointing to "hello-world"
    // argv[2] => 2
    // argv[3] => 3 
 }
aeh
Works perfectly
Mark
probably should use `escapeshellarg()` if the arguments come from a user - http://php.net/escapeshellarg
tmont
True, almost forgot
Mark