C++: Getting a temporary file, cross-platform

The Boost Filesystem library, from version 3 of that library, can be used to create a temporary file name. It also offers a crisp solution. Indeed, the following C++ code should be platform independent:

// Boost.Filesystem VERSION 3 required
#include <string>
#include <boost/filesystem.hpp>
boost::filesystem::path temp = boost::filesystem::unique_path();
const std::string tempstr    = temp.native();  // optional

The filesystem path object temp can be used to open a file or create a subdirectory, while the string object tempstr offers the same information as a string.


If you use Qt: QTemporaryFile class is perfect.