c ++ unzip返回无法创建提取目录

时间:2013-02-08 07:27:53

标签: c++ fork unzip execvp

我编写了一段简单的代码,用于解压缩zip文件。没有设置输出目录但返回错误是设置了目录

,它工作正常
  

“存档:/home/vishvesh.kumar/tempFolder/test.zip checkdir:不能   创建提取目录:/home/vishvesh.kumar/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 个答案:

答案 0 :(得分:2)

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

应该是

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

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

答案 1 :(得分: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之后的参数。所以它试图创建该目录而不能,因为它是一个文件。