How-To generate temporary file names

POSIX and WIN32 both provide APIs for retrieving the name of a directory in which temporary files may be stored. Of course, both APIs work very differently from each other. Additionally, actually creating temporary file can be vulnerable – in particular, security guarantees one could make are not portable.

Warning

The functions in this section are not secure, but they do provide reasonably unique file names. If you need a secure function, please consider a different approach.

The main point of this function is to provide a file name that is in the system’s temporary directory, and should not typically collide with other attempts to create such files.

Create temporary names
1#include <liberate/fs/tmp.h>
2
3auto tmp = liberate::fs::temp_name("my-prefix");

The prefix argument is optional and may be omitted.