如何在C++中创建临时目录?

Igo*_*tis 9 c++ linux winapi temporary-directory

我正在用C++编写一个函数来创建一个临时目录.这样的功能应该尽可能地便携,例如它应该在linux,mac和win32环境下工作.我如何实现这一目标?

Sep*_*rvi 18

Boost Filesystem Library的第3版提供unique_path()了生成适合创建临时文件或目录的路径名的功能.

using namespace boost::filesystem;

path ph = unique_path();
create_directories(ph);
Run Code Online (Sandbox Code Playgroud)


Cir*_*四事件 9

C++17 std::filesystem::temp_directory_path+ 随机数生成

以下是一个纯粹的C ++ 17的解决方案,可能是可靠:无升压或其他外部库并且没有mkdtemp其是POSIX

我们只是循环随机数,直到我们能够创建一个之前不存在的目录std::filesystem::temp_directory_path/tmp在 Ubuntu 18.04 中)。

然后,我们可以std::filesystem::remove_all在完成后显式删除创建的目录。

我不确定 C++ 标准是否能保证这一点,但极有可能std::filesystem::temp_directory_path调用mkdir,它以原子方式尝试创建目录,如果它不能失败EEXIST,则我认为并行调用者之间不会存在竞争条件。

主程序

#include <exception>
#include <fstream>
#include <iostream>
#include <random>
#include <sstream>

#include <filesystem>

std::filesystem::path create_temporary_directory(
      unsigned long long max_tries = 1000) {
    auto tmp_dir = std::filesystem::temp_directory_path();
    unsigned long long i = 0;
    std::random_device dev;
    std::mt19937 prng(dev());
    std::uniform_int_distribution<uint64_t> rand(0);
    std::filesystem::path path;
    while (true) {
        std::stringstream ss;
        ss << std::hex << rand(prng);
        path = tmp_dir / ss.str();
        // true if the directory was created.
        if (std::filesystem::create_directory(path)) {
            break;
        }
        if (i == max_tries) {
            throw std::runtime_error("could not find non-existing directory");
        }
        i++;
    }
    return path;
}

int main() {
    auto tmpdir = create_temporary_directory();
    std::cout << "create_temporary_directory() = "
              << tmpdir
              << std::endl;

    // Use our temporary directory: create a file
    // in it and write to it.
    std::ofstream ofs(tmpdir / "myfile");
    ofs << "asdf\nqwer\n";
    ofs.close();

    // Remove the directory and its contents.
    std::filesystem::remove_all(tmpdir);
}
Run Code Online (Sandbox Code Playgroud)

GitHub 上游.

编译并运行:

g++-8 -std=c++17 -Wall -Wextra -pedantic -o main.out main.cpp -lstdc++fs
./main.out
Run Code Online (Sandbox Code Playgroud)

示例输出:

_directory.out
temp_directory_path() = "/tmp"
create_temporary_directory() = "/tmp/106adc08ff89874c"
Run Code Online (Sandbox Code Playgroud)

有关文件,请参阅:如何在 C++ 中创建临时文本文件?文件有点不同,因为open在 Linux 中有O_TMPFILE,它创建一个匿名 inode,它在 close 时自动消失,因此专用临时文件 API 可以通过使用它来提高效率。mkdir然而,没有类似的标志,所以这个解决方案可能是最佳的。

在 Ubuntu 18.04 中测试。


小智 6

这里检查mkdtemp功能。