tmpnam_s、_wtmpnam_stmpnam_s, _wtmpnam_s

生成可用于创建临时文件的名称。Generate names you can use to create temporary files. 这些版本的 tmpnam 和 _wtmpnam 具有安全增强功能,如 CRT 中的安全功能中所述。These are versions of tmpnam and _wtmpnam with security enhancements as described in Security Features in the CRT.

语法Syntax

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

参数Parameters

strstr
保留生成的名称的指针。Pointer that will hold the generated name.

sizeInCharssizeInChars
以字符为单位的缓冲区的大小。The size of the buffer in characters.

返回值Return Value

如果成功,则这两个函数均返回 0,如果失败,则返回错误号。Both of these functions return 0 if successful or an error number on failure.

错误条件Error Conditions

strstr sizeInCharssizeInChars 返回值Return Value 内容 strContents of str
NULLNULL 任何any EINVALEINVAL 未修改not modified
Not NULL (指向有效内存)not NULL (points to valid memory) 过短too short ERANGEERANGE 未修改not modified

如果strNULL,则将调用无效参数处理程序,如参数验证中所述。If str is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续, 则这些函数会将errno设置为EINVAL并返回EINVALIf execution is allowed to continue, these functions set errno to EINVAL and return EINVAL.

备注Remarks

这些函数返回的文件名当前不存在。Each of these functions returns the name of a file that does not currently exist. tmpnam_s返回GetTempPathW返回的指定 Windows 临时目录中的唯一名称。tmpnam_s returns a name unique in the designated Windows temporary directory returned by GetTempPathW. 请注意,如果一个文件名称使用反斜杠作为前缀(如 \fname21),表示该名称对当前工作目录有效。Note than when a file name is pre-pended with a backslash and no path information, such as \fname21, this indicates that the name is valid for the current working directory.

对于tmpnam_s,你可以将生成的文件名存储在str中。For tmpnam_s, you can store this generated file name in str. Tmpnam_s返回的字符串的最大长度为L_TMPNAM_S,在 stdio.h 中定义。高.The maximum length of a string returned by tmpnam_s is L_tmpnam_s, defined in STDIO.H. 如果strNULL,则tmpnam_s将结果保留在内部静态缓冲区中。If str is NULL, then tmpnam_s leaves the result in an internal static buffer. 因此,任何后续调用都会破坏该值。Thus any subsequent calls destroy this value. tmpnam_s生成的名称包含程序生成的文件名,并在第一次调用tmpnam_s后,在第32中为序列号的文件扩展名,在 stdio.h 中为TMP_MAX_S 。H 为INT_MAX)。The name generated by tmpnam_s consists of a program-generated file name and, after the first call to tmpnam_s, a file extension of sequential numbers in base 32 (.1-.1vvvvvu, when TMP_MAX_S in STDIO.H is INT_MAX).

tmpnam_s会根据需要自动处理多字节字符串参数,根据从操作系统获取的 OEM 代码页识别多字节字符序列。tmpnam_s automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the OEM code page obtained from the operating system. _wtmpnam_stmpnam_s的宽字符版本; _wtmpnam_s的参数和返回值是宽字符字符串。_wtmpnam_s is a wide-character version of tmpnam_s; the argument and return value of _wtmpnam_s are wide-character strings. _wtmpnam_stmpnam_s的行为相同,只不过 _wtmpnam_s不处理多字节字符字符串。_wtmpnam_s and tmpnam_s behave identically except that _wtmpnam_s does not handle multibyte-character strings.

在 C++ 中,通过模板重载简化这些函数的使用;重载可以自动推导出缓冲区长度,不再需要指定大小参数。In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically, eliminating the need to specify a size argument. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_ttmpnam_s_ttmpnam_s tmpnam_stmpnam_s tmpnam_stmpnam_s _wtmpnam_s_wtmpnam_s

要求Requirements

例程所返回的值Routine 必需的标头Required header
tmpnam_stmpnam_s <stdio.h><stdio.h>
_wtmpnam_s_wtmpnam_s <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// 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.

请参阅See also

流 I/OStream I/O
_getmbcp_getmbcp
mallocmalloc
_setmbcp_setmbcp
tmpfile_stmpfile_s