什么是最佳实践方式?
tmpnam() 返回一个位于根目录的文件路径,在 Windows Vista 上需要管理员权限,因此这不是一个选项。
在Windows上,环境变量%TEMP%指向用户的临时目录。
在托管C++中,您可以调用Path::GetTempFileName(),它将为您提供一个位于用户临时目录中的临时文件(可以使用Path::GetTempPath()找到该目录)。 GetTempFileName()基本上只是使用GUID作为文件名,在%TEMP%路径中为您提供一个文件路径。 然后,您可以使用该路径创建文件并对其进行操作。 您可以在任何具有访问当前进程环境变量的语言中执行类似的逻辑。
希望这有所帮助,
Martin。
如果您关心互操作性,tmpnam的man页面建议:
BUGS
Never use this function. Use mkstemp(3) instead.
SYNOPSIS
#include <stdlib.h>
int mkstemp(char *template);
DESCRIPTION
The mkstemp() function generates a unique temporary file name from template. The last six characters of template must be
XXXXXX and these are replaced with a string that makes the filename unique. The file is then created with mode read/write
但所有这些都表明您已经准备好了以TMP环境变量的内容为前缀的模板。