tmpnam_s_wtmpnam_s

生成可用于创建临时文件的名称。 这些函数的版本是 tmpnam_wtmpnam,具有安全性增强功能,如 CRT 中的安全功能中所述。

语法

errno_t tmpnam_s(
   char * str,
   size_t sizeInChars
);
errno_t _wtmpnam_s(
   wchar_t *str,
   size_t sizeInChars
);
template <size_t size>
errno_t tmpnam_s(
   char (&str)[size]
); // C++ only
template <size_t size>
errno_t _wtmpnam_s(
   wchar_t (&str)[size]
); // C++ only

参数

str
[out] 保存生成的名称的指针。

sizeInChars
[in] 以字符为单位的缓冲区的大小。

返回值

如果成功,则这两个函数均返回 0,如果失败,则返回错误号。

错误条件

str sizeInChars 返回值 str 的内容
NULL any EINVAL 未修改
NULL(指向有效内存) 过短 ERANGE 未修改

如果 strNULL,则会调用无效的参数句柄,如参数验证中所述。 如果允许执行继续,则这些功能将 errno 设置为 EINVAL 并返回 EINVAL

备注

这些函数返回的文件名当前不存在。 tmpnam_s 返回由 GetTempPathW 返回的指定 Windows 临时目录中的唯一名称。 当文件名前面加上反斜杠且没有路径信息(如 \fname21),则表示该名称对当前工作目录有效。

对于 tmpnam_s,可以在 str 中存储生成的此文件名。 由 tmpnam_s 返回的字符串的最大长度为 STDIO.H 中定义的 L_tmpnam_s。 如果 strNULL,则 tmpnam_s 将结果留在内部静态缓冲区中。 因此,任何后续调用都会破坏该值。 由 tmpnam_s 生成的名称包含程序生成的文件名,以及第一次调用 tmpnam_s 后,基数 32 连续数字的文件扩展名(当 STDIO.H 中的 TMP_MAX_SINT_MAX 时,扩展名为 .1-.1vvvvvu)。

tmpnam_s 将根据情况自动处理多字节字符串参数,根据从操作系统获取的 OEM 代码页识别多字节字符序列。 _wtmpnam_stmpnam_s 的宽字符版本;_wtmpnam_s 的参数和返回值都是宽字符字符串。 _wtmpnam_stmpnam_s 的行为方式相同,只不过 _wtmpnam_s 不处理多字节字符字符串。

在 C++ 中,通过模板重载简化这些函数的使用;重载可以自动推导出缓冲区长度,不再需要指定大小参数。 有关详细信息,请参阅安全模板重载

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

一般文本例程映射

TCHAR.H 例程 _UNICODE_MBCS 未定义 _MBCS 已定义 _UNICODE 已定义
_ttmpnam_s tmpnam_s tmpnam_s _wtmpnam_s

要求

例程 必需的标头
tmpnam_s <stdio.h>
_wtmpnam_s <stdio.h> 或 <wchar.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_tmpnam_s.c
// This program uses tmpnam_s to create a unique filename in the
// current working directory.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char name1[L_tmpnam_s];
   errno_t err;
   int i;

   for (i = 0; i < 15; i++)
   {
      err = tmpnam_s( name1, L_tmpnam_s );
      if (err)
      {
         printf("Error occurred creating unique filename.\n");
         exit(1);
      }
      else
      {
         printf( "%s is safe to use as a temporary file.\n", name1 );
      }
   }
}
C:\Users\LocalUser\AppData\Local\Temp\u19q8.0 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.1 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.2 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.3 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.4 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.5 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.6 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.7 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.8 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.9 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.a is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.b is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.c is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.d is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.e is safe to use as a temporary file.

另请参阅

流 I/O
_getmbcp
malloc
_setmbcp
tmpfile_s