gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:18:51 28 4
gpt4 key购买 nike

我编写了一段简单的代码,用于使用 unzip 提取 zip 文件。当未设置输出目录时它工作正常但返回错误是目录已设置

"Archive: /home/vishvesh.kumar/tempFolder/test.zip checkdir: cannot create extraction directory: /home/vishvesh.kumar/tempFolder No such file or directory"

代码:

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();
}
}

最佳答案

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 之后的参数。因此它会尝试创建该目录但不能,因为它是一个文件。

关于c++ unzip 返回无法创建提取目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14767650/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com