C++ run secondary program, only if the command is typed

Asked

Viewed 145 times

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, however I would like to insert an argument, so that it runs. Like only open the program if you type in a keyword, is it possible? How would it look?

Thank you.

1 answer

1


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

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 it will call iapt2.exe will stay:

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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.