How to create a std::ofstream to a temp file?

后端 未结 4 1385
醉梦人生
醉梦人生 2020-12-24 12:45

Okay, mkstemp is the preferred way to create a temp file in POSIX.

But it opens the file and returns an int, which is a file descriptor. F

4条回答
  •  太阳男子
    2020-12-24 13:14

    char tempFileName[20]; // name only valid till next invocation of tempFileOpen
    ofstream tempFile;
    void tempFileOpen()
    {
        strcpy(tempFileName, "/tmp/XXXXXX");
        mkstemp(tempFileName);
        tempFile.open(tempFileName);
    }
    

    This code works for me with GCC/libstdc++6 4.8.4 and Clang 3.9 as well. Thanks to the other replies as well which were helpful to me.

提交回复
热议问题