0

我正在尝试使用 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;
}
4

1 回答 1

1

哇……错别字。

其中 argv[0] = "sep";

应该是“scp”

    char** args;

    args = new char*[argc+2]; //forcing 2 flags
    args[0] = "scp";
    args[1] = "-i";
    args[2] = "-r";
    args[3] = argv[1]; //key
    args[4] = argv[2]; //source
    args[5] = argv[3]; //destination

    execvp(args[0], args);
于 2013-04-19T18:49:05.730 回答