2010-09-04 28 views
2

Je le code suivant qui exécute un C++ programme et sorties il:système PHP() args

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

Comment puis-je faire en sorte que vous pouvez transmettre des arguments au C++ programme , dire comme ça ...

Si tel était le programme C++

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

Pourrais-je faire quelque chose comme ça?

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

Je ne sais pas beaucoup de pièces à ce problème, je peux exécuter le programme mais je besoin de passer des arguments.

Répondre

1

Vous pouvez passer l'espace d'arguments séparés après la commande comme system("app.exe hello-world 2 3", $out);

dans votre C++ programme

int main (int argc, char** argv) { 
    // argv[1] will be pointing to "hello-world" 
    // argv[2] => 2 
    // argv[3] => 3 
} 
+0

Fonctionne parfaitement –

+1

devraient probablement utiliser 'escapeshellarg()' si les arguments proviennent d'un utilisateur - http://php.net/escapeshellarg – tmont

+0

Vrai, presque oublié –