Is it possible to get the file name (and path) from a call to mkstemp()
? And if "yes", how?
Get the file name that was created by mkstemp()
4.9k Views Asked by alexandernst At
2
There are 2 best solutions below
0

The input string is modified to the file name. Consequently, it cannot be a string literal.
POSIX says of mkstemp()
:
#include <stdlib.h> int mkstemp(char *template);
The
mkstemp()
function shall replace the contents of the string pointed to bytemplate
by a unique pathname, and return a file descriptor for the file open for reading and writing. … The string intemplate
should look like a pathname with six trailing 'X' s;mkstemp()
replaces each 'X' with a character from the portable filename character set. …
The same page also describes mkdtemp()
which can be used to create a temporary directory.
From the
mkstemp
manual page:So you declare an array and pass it to the function, which will modify it, and then you have the filename in the array.