Using /tmp in with C++ scripts

How does one create data files to write to in the /tmp directory in a C++ script?
If the tmp directory already exists, this should be reasonably trivial.

1
2
3
4
5
6
7
8
9
#include <fstream>

int main(int argc, const char* argv[])
{
    std::ofstream os("tmp/somefile.txt");
    os << "Bleh";
    
    return 0;
}


If the directory doesn't exist, it's a little more fiddly as there's no standard way of creating directories - your best bet is to call into the OS APIs to do so.
If you don't need the temporary file to persist, there is a standard C++ (and C) function tmpfile
http://en.cppreference.com/w/cpp/io/c/tmpfile and http://www.cplusplus.com/reference/cstdio/tmpfile

The location of the tmp directory can be obtained with temp_directory_path from boost (also part of the upcoming C++17
http://www.boost.org/doc/libs/release/libs/filesystem/doc/reference.html#temp_directory_path
http://en.cppreference.com/w/cpp/filesystem/temp_directory_path
It is not clear to me at all how to do any of this. For example, even if I know how to create a temporary file, how do I then transfer that temporary file back to somewhere in my home directory? How can I specify a particular path name to which the file should be transfered afterward?
Topic archived. No new replies allowed.