2

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 Answer 1

1

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 
 }
Sign up to request clarification or add additional context in comments.

1 Comment

probably should use escapeshellarg() if the arguments come from a user - php.net/escapeshellarg

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.