简体   繁体   中英

Get the file name that was created by mkstemp()

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM