Log in

View Full Version : EXEC: Multi-line input



Jas
06-16-2008, 04:43 PM
I created my very first C++ program :) And now I want to call it from PHP. The problem that I have is that the inputs are on different lines. I.E.:

program.exe
What's your name: <name>
What's your username: <username>
output here
How can I run an exec() function to fill in those inputs? I tried variations of:

echo exec("cd path/to/program/ && program.exe\nJas\nJas\n");
and

$out = array();
echo exec("cd path/to/program/ && program.exe\nJas\nJas\n",$out);
print_r($out);
But it doesn't work. Any thoughts?

If this won't work, is there an alternate way to input those without using a flat file or putting all the vars on one line?

Master_script_maker
06-16-2008, 05:48 PM
try
$out = array();
echo exec("cd path/to/program/ && program.exe -u username -p password",$out);
print_r($out);
and in c++ you will access the command line arguments. an example (will store text after -p as pass and text after -u as user):
int main( int argc, char *argv[] ) {
if(argc>1) {
int i=1;
while ((i<argc) && (argv[i][0]=='-')) {
string val=argv[i];
if(val=="-u") {
i++;
string user=argv[i];
} else if(val=="-p") {
i++;
string pass=argv[i];
}
i++;
}
}
}

Jas
06-18-2008, 03:36 AM
Thank you very much for that chunk of code :)

How does the command prompt process that string? For example:

cd path/to/program/ && program.exe -u Jas|is|cool -p "really|really"|cool
(I'm not shallow at all ;))
Will that work as expected?

Master_script_maker
06-18-2008, 01:13 PM
the command prompt send the arguments to the program main, and you can choose to recieve them. if you do, it sends all the arguments in order including the program name in the array argv, along with the number of arguments in argc. the code loops through the arguments, except the first one and tries to find anything beginning with a '-' if it does it will match which delimiter it is and add the text after it to a string variable.

Jas
06-18-2008, 03:40 PM
That's not quite what I am looking for, but I'll give it a try. Thanks.