I have to write a program that will start a new program called demo.cpp using execvp when user inputs certain commands. User will enter input in the format
bg demo word int1 int2 (ignore the word relevance of the word bg, I store it in a string called command in my code).
This is the code :
int main(){
char *argv[5];
argv[0] = new char[10];
argv[1] = new char[10];
argv[2] = new char[10];
argv[3] = new char[10];
argv[4] = nullptr;
string command;
pid_t PID;
cin>>command; //store the string 'bg'
cin>>argv[0]>>argv[1]>>argv[2]>>argv[3];
PID = fork();
if(PID<0){
perror("Fork failed!");
}
else if(PID==0){
if(execvp("./demo.cpp",argv)<0)
printf("Failed");
}
wait(0);
return 0;
}
Every time the code prints "Failed". The only thing I can think is that maybe there's an error in the format of arguments passed to execvp. This code and demo.cpp are stored in the same folder.