2013-02-08 20 views
0

我写了一段简单的代码,用于使用unzip提取zip文件。它工作正常,如果没有设置输出目录,但返回错误的目录设置C++ unzip返回无法创建提取目录

“归档:/home/vishvesh.kumar/tempFolder/test.zip checkdir:不能 创建解压缩目录:/家庭/ vishvesh。库马尔/ tempFolder 没有这样的文件或目录”

代码:

int main(int argc, char *argv1[]) 
{ 
    std::cout << "Creating child process..."; 
    std::vector<std::string> arguements; 

    char* argv[4] = {0}; 
    argv [0] = "/usr/bin/unzip"; 
    argv [2] = "/home/vishvesh.kumar/tempFolder/test.zip"; 
    argv [1] = "-d /home/vishvesh.kumar/tempFolder/"; 
    argv [3] = NULL; 
    createChildProcess(argv); 

} 


void createChildProcess(char* argv[]) 
{ 
    pid_t pid = fork(); 
    if (pid == -1) 
    { 
      std::cout << "error creating child process, exiting..."; 
      exit(1); 
    } 
    else if (pid == 0) 
    { // This is the child process 
     std::cout << "This is the child process. About to sleep\n"; 
     std::cout << "Woke up\n"; 
     if (execvp(argv[0], argv)) 
     { // execvp failed 
      std::cout << "fatal - execvp failed!"; 
      exit(1); 
     } 

    } 
    else 
    { // This is the parent process. 
     std::cout << "This is the parent process\n"; 
     int status; 
     std::cout << " done. PID: " << pid << ".\n"; 

     double start = 0; 
     waitpid(pid, &status, 0); // Wait for program to finish executing. 
     double dur = (clock() - start)/CLOCKS_PER_SEC; // Get execution time 
     std::cout << "Sad my son died\n"; 
     std::cout << "Program returned " << WEXITSTATUS(status); 
     std::cout << " and lasted " << dur << " seconds.\nPress enter.\n"; 
     std::cin.get(); 
    } 
} 

回答

2
char* argv[4] = {0}; 
argv [0] = "/usr/bin/unzip"; 
argv [2] = "/home/vishvesh.kumar/tempFolder/test.zip"; 
argv [1] = "-d /home/vishvesh.kumar/tempFolder/"; 
argv [3] = NULL; 

应该是:

char* argv[5]; 
argv [0] = "/usr/bin/unzip"; 
argv [1] = "-d"; 
argv [2] = "/home/vishvesh.kumar/tempFolder/"; 
argv [3] = "/home/vishvesh.kumar/tempFolder/test.zip"; 
argv [4] = NULL; 

你有它的方式,参数-d是zip文件,因为这是在-d之后的参数。所以它会尝试创建该目录,因为它是一个文件而不能。

+0

非常感谢。有效。 – Vishvesh

2
argv [1] = "-d /home/vishvesh.kumar/tempFolder/"; 

应该是

argv [1] = "-d"; 
argv [2] = "/home/vishvesh.kumar/tempFolder/"; 

它们是通过execvp()传递给新过程的两个独立参数。

+0

我试过了,但没有奏效。有以下错误。警告:文件名不匹配:/home/vishvesh.kumar/tempFolder/ – Vishvesh