0

我编写了一段简单的代码,用于使用 unzip 提取 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();
    }
}
4

2 回答 2

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

应该

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

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

于 2013-02-08T07:31:11.247 回答
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. 所以它试图创建该目录并且不能因为它是一个文件。

于 2013-02-08T07:31:51.947 回答