获取由 mkstemp() 创建的文件名

Get the file name that was created by mkstemp()

是否可以通过调用 mkstemp() 获取文件名(和路径)?如果 "yes",如何?

输入字符串修改为文件名。因此,它不能是字符串文字。

POSIX 说 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. …

同一页还描述了 mkdtemp() 可用于创建临时目录。

来自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.

所以你声明一个数组并将它传递给函数,函数将修改它,然后你在数组中有文件名。