Get the file name that was created by mkstemp()

4.9k Views Asked by At

Is it possible to get the file name (and path) from a call to mkstemp()? And if "yes", how?

2

There are 2 best solutions below

4
On BEST ANSWER

From the mkstemp manual page:

The last six characters of template must be "XXXXXX" and these are replaced with a string that makes the filename unique. Since it will be modified, template must not be a string constant, but should be declared as a character array.

So you declare an array and pass it to the function, which will modify it, and then you have the filename in the array.

0
On

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 by template by a unique pathname, and return a file descriptor for the file open for reading and writing. … The string in template 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.