Search code examples
c++linuxtemporary-files

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


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.


Solution

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

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