我正在尝试使用 c++ 程序运行 sep 命令。当我编译下面的代码时,我收到一条警告,argv[0]="sep";
说明"deprecated conversion from string constant to âchar*â [-Wwrite-strings]."
当我运行下面的程序时,我得到 Exec Failed!每次从 execvp() 下面的行开始。
#include<iostream>
#include<string>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int main(int argc, char **argv){
pid_t pid;
int fail;
argv[0] = "sep";
int i=0;
while(i < argc){
cout<< i << ": " << argv[i] <<endl;
i++;
}
if(argc < 5){
cout<< "./upload -i -r <key> <source> <destination>" <<endl;
}else{
pid = fork();
if(pid < 0){
cout<<"Fork Failed!\n";
exit(1);
}else if(pid == 0){ //if you are in the child process
fail = execvp("scp", argv); //execute command, return -1 on fail
cout<< "Exec Failed!\n";
exit(1);
}else{
int status;
waitpid(pid, &status, 0); //wait for each pid
}
}
return 0;
}