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

[out] str[out] str
保留生成的名称的指针。Pointer that will hold the generated name.

[in] sizeInChars[in] sizeInChars
以字符为单位的缓冲区的大小。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

str sizeInChars 返回值Return Value str 的内容Contents of str
NULL 任何any EINVAL 未修改not modified
NULL(指向有效内存)not NULL (points to valid memory) 过短too short ERANGE 未修改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 返回当前工作目录中唯一的名称。tmpnam_s returns a name unique in the current working directory. 请注意,如果一个文件名称使用反斜杠作为前缀(如 \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 返回的字符串的最大长度为 STDIO.H 中定义的 L_tmpnam_sThe 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 为 INT_MAX 时,扩展名为 .1-.1vvvvvu)。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 tmpnam_s tmpnam_s _wtmpnam_s

惠?Requirements

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

有关其他兼容性信息,请参见“简介”中的 兼容性For additional compatibility information, see Compatibility in the Introduction.

示例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 );  
      }  
   }    
}  

请参阅See Also

流 I/O Stream I/O
_getmbcp _getmbcp
malloc malloc
_setmbcp _setmbcp
tmpfile_stmpfile_s