How to create a temporary directory in C++?

Igor Gatis picture Igor Gatis · Aug 1, 2010 · Viewed 22.2k times · Source

I'm writing a function in C++ which creates a temporary directory. Such function should be as most portable as possible, e.g. it should work under linux, mac and win32 environments. How do I achieve that?

Answer

Seppo Enarvi picture Seppo Enarvi · May 13, 2011

Version 3 of Boost Filesystem Library provides function unique_path() for generating a path name suitable for creating a temporary file or directory.

using namespace boost::filesystem;

path ph = temp_directory_path() / unique_path();
create_directories(ph);