如何为临时文件创建std :: ofstream?

Fra*_*ank 36 c++ file-io file mkstemp

好的,mkstemp是在POSIX中创建临时文件的首选方法.

但它会打开文件并返回一个int文件描述符.从那以后我只能创建一个FILE*,而不是一个std::ofstream,我更喜欢C++.(显然,在AIX和其他一些系统上,您可以std::ofstream从文件描述符创建一个,但是当我尝试时,我的编译器会抱怨.)

我知道我可以获得一个临时文件名,tmpnam然后用它打开我自己的ofstream,但由于竞争条件,这显然是不安全的,并导致编译器警告(Linux上的g ++ v3.4.):

warning: the use of `tmpnam' is dangerous, better use `mkstemp'
Run Code Online (Sandbox Code Playgroud)

那么,是否有任何可移植的方法来创建std::ofstream临时文件?

Joh*_*itb 16

我做过这个功能:

#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <vector>

std::string open_temp(std::string path, std::ofstream& f) {
    path += "/XXXXXX";
    std::vector<char> dst_path(path.begin(), path.end());
    dst_path.push_back('\0');

    int fd = mkstemp(&dst_path[0]);
    if(fd != -1) {
        path.assign(dst_path.begin(), dst_path.end() - 1);
        f.open(path.c_str(), 
               std::ios_base::trunc | std::ios_base::out);
        close(fd);
    }
    return path;
}

int main() {
    std::ofstream logfile;
    open_temp("/tmp", logfile);
    if(logfile.is_open()) {
        logfile << "hello, dude" << std::endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

您应该确保使用正确的文件创建掩码调用umask(我更喜欢0600) - mkstemp的联机帮助页说文件模式创建掩码不是标准化的.它使用mkstemp将其参数修改为它使用的文件名的事实.因此,我们打开它并关闭它打开的文件(因此,不要打开它两次),留下一个连接到该文件的ofstream.


f3l*_*lix 11

我认为这应该有效:

    char *tmpname = strdup("/tmp/tmpfileXXXXXX");
    ofstream f;
    int fd = mkstemp(tmpname);
    f.attach(fd);
Run Code Online (Sandbox Code Playgroud)

编辑:嗯,这可能不是便携式的.如果您不能使用attach而无法直接从文件描述符创建ofstream,那么您必须这样做:

char *tmpname = strdup("/tmp/tmpfileXXXXXX");
mkstemp(tmpname);
ofstream f(tmpname);
Run Code Online (Sandbox Code Playgroud)

由于mkstemp已经为您创建了文件,因此竞争条件不应成为问题.

  • `mkstemp`可以失败并返回-1; 你的代码应该抓住这种情况. (3认同)
  • 使用`strdup`之后你需要调用`free`. (3认同)