C ++ execute secondary program, only if the command is entered

2
int main()
{
system("F:\AI\iapt2.exe");
system("pause");

    return 0;

}

Good evening, I have this code right, it will open the program iapt2.exe when running, but I would like to insert an argument, so that it runs. Type just open the program if you type a keyword, is it possible? How would it look?

Thank you.

    
asked by anonymous 05.06.2016 / 06:33

1 answer

1

If the parameter is directed to the executable you are trying to open it would look like:

char command[512];
char arg[64];
scanf("%s", arg);
sprintf(command, "F:\AI\iapt2.exe %s", arg); // executa iapt2.exe com o parametro dentro de arg
system(command);

If it is a parameter passed to your program to define whether to call iapt2.exe will be:

int main(int argc, char **argv){ // argc - quantidade de parâmetros passados
                                 // argv - parâmetros passados
    if(!strcmp(argv[argc-1], "buiatchaca") // argv[argc-1] pega o ultimo parâmetro digitado
        system("F:\AI\iapt2.exe");
    system("pause");

    return 0;
}
    
05.06.2016 / 08:13