生成可用于创建临时文件的名称。 提供这些函数的更安全版本;请参阅 tmpnam_s
、_wtmpnam_s
。
语法
char *_tempnam(
const char *dir,
const char *prefix
);
wchar_t *_wtempnam(
const wchar_t *dir,
const wchar_t *prefix
);
char *tmpnam(
char *str
);
wchar_t *_wtmpnam(
wchar_t *str
);
参数
prefix
_tempnam
返回的名称前面附加的字符串。
dir
文件名中使用的路径(如果不存在 TMP 环境变量或 TMP 不是有效的目录)。
str
保存生成的名称的指针,与函数返回的名称相同。 保存生成的名称是一种便捷的方法。
返回值
如果失败,则每个函数均返回一个指向生成的名称的指针或 NULL
。 如果尝试使用 tmpnam
执行超过 TMP_MAX
次调用(请参阅 STDIO.H),或如果使用 _tempnam
,但在 TMP
环境变量和 dir
参数中指定了无效的目录名称,则可能失败。
注意
由 tmpnam
和 _wtmpnam
返回的指针指向内部静态缓冲区。 不应调用 free
来解除分配这些指针。 对于由 _tempnam
和 _wtempnam
分配的指针,需要调用 free
。
注解
这些函数返回的文件名当前不存在。 tmpnam
返回由 GetTempPathW
返回的指定 Windows 临时目录中的唯一名称。 _tempnam
在指定目录以外的目录中生成唯一名称。 当文件名前面加上反斜杠且没有路径信息(如 \fname21
),则表示该名称对当前工作目录有效。
对于 tmpnam
,可以在 str
中存储生成的此文件名。 如果 str
为 NULL
,则 tmpnam
将结果留在内部静态缓冲区中。 因此,任何后续调用都会破坏该值。 由 tmpnam
生成的名称包含程序生成的文件名以及第一次调用tmpnam
后,基数 32 中的连续数字的文件扩展名(当 STDIO.H 中的 TMP_MAX
为 32,767 时,扩展名为 .1-.vvu)。
_tempnam
将按以下规则,为所选目录生成唯一文件名:
如果定义了 TMP 环境变量并将其设置为有效的目录名称,则 TMP 所指定的目录将生成唯一文件名。
如果未定义 TMP 环境变量,或为其设置了不存在的目录名称,则
_tempnam
将dir
参数用作生成唯一名称的路径。如果未定义 TMP 环境变量,或为其设置了并不存在的目录名称,且如果
dir
为NULL
或被设置为不存在的目录名称,则_tempnam
将使用当前工作目录生成唯一名称。 目前,如果 TMP 和dir
所指定的目录的名称不存在,则 _tempnam 函数调用将失败。
由 _tempnam
返回的名称将是 prefix
和顺序号的串联,组合为指定目录创建唯一的文件名。 _tempnam
生成无扩展名的文件名。 _tempnam
使用 malloc
为文件名分配空间;当不再需要此空间时,程序负责释放此空间。
_tempnam
和 tmpnam
将根据情况自动处理多字节字符串参数,从而根据从操作系统获取的 OEM 代码页识别多字节字符序列。 _wtempnam
是 _tempnam
的宽字符版本;_wtempnam
的参数和返回值都是宽字符字符串。 _wtempnam
和 _tempnam
的行为方式相同,只不过 _wtempnam
不处理多字节字符字符串。 _wtmpnam
是 tmpnam
的宽字符版本;_wtmpnam
的参数和返回值都是宽字符字符串。 _wtmpnam
和 tmpnam
的行为方式相同,只不过 _wtmpnam
不处理多字节字符字符串。
如果定义了 _DEBUG
和 _CRTDBG_MAP_ALLOC
,则 _tempnam
和 _wtempnam
将替换为对 _tempnam_dbg
和 _wtempnam_dbg
的调用。
一般文本例程映射
TCHAR.H 例程 | _UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
---|---|---|---|
_ttmpnam |
tmpnam |
tmpnam |
_wtmpnam |
_ttempnam |
_tempnam |
_tempnam |
_wtempnam |
要求
例程 | 必需的标头 |
---|---|
_tempnam |
<stdio.h> |
%> | <stdio.h> 或 <wchar.h> |
tmpnam |
<stdio.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_tempnam.c
// compile with: /W3
// This program uses tmpnam to create a unique filename in the
// temporary directory, and _tempname to create a unique filename
// in C:\\tmp.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char * name1 = NULL;
char * name2 = NULL;
char * name3 = NULL;
// Create a temporary filename for the current working directory:
if ((name1 = tmpnam(NULL)) != NULL) { // C4996
// Note: tmpnam is deprecated; consider using tmpnam_s instead
printf("%s is safe to use as a temporary file.\n", name1);
} else {
printf("Cannot create a unique filename\n");
}
// Create a temporary filename in temporary directory with the
// prefix "stq". The actual destination directory may vary
// depending on the state of the TMP environment variable and
// the global variable P_tmpdir.
if ((name2 = _tempnam("c:\\tmp", "stq")) != NULL) {
printf("%s is safe to use as a temporary file.\n", name2);
} else {
printf("Cannot create a unique filename\n");
}
// When name2 is no longer needed:
if (name2) {
free(name2);
}
// Unset TMP environment variable, then create a temporary filename in C:\tmp.
if (_putenv("TMP=") != 0) {
printf("Could not remove TMP environment variable.\n");
}
// With TMP unset, we'll use C:\tmp as the temporary directory.
// Create a temporary filename in C:\tmp with prefix "stq".
if ((name3 = _tempnam("c:\\tmp", "stq")) != NULL) {
printf("%s is safe to use as a temporary file.\n", name3);
}
else {
printf("Cannot create a unique filename\n");
}
// When name3 is no longer needed:
if (name3) {
free(name3);
}
return 0;
}
C:\Users\LocalUser\AppData\Local\Temp\sriw.0 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\stq2 is safe to use as a temporary file.
c:\tmp\stq3 is safe to use as a temporary file.