How can I create a temporary file for writing in C++ on a Linux platform?

J Miller picture J Miller · Oct 2, 2008 · Viewed 8.2k times · Source

In C++, on Linux, how can I write a function to return a temporary filename that I can then open for writing?

The filename should be as unique as possible, so that another process using the same function won't get the same name.

Answer

nobody picture nobody · Oct 2, 2008

Use one of the standard library "mktemp" functions: mktemp/mkstemp/mkstemps/mkdtemp.

Edit: plain mktemp can be insecure - mkstemp is preferred.