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 内容str Contents of str
NULLNULL 任何any EINVALEINVAL 未修改not modified
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. 如果允许执行继续,则这些函数将设置errnoEINVAL并返回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,你可以存储在此生成的文件名称strFor tmpnam_s, you can store this generated file name in str. 通过返回的字符串的最大长度tmpnam_sL_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 文件扩展名 (.1.1vvvvvu,当TMP_MAX_S STDIO 中。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_s是宽字符版本的tmpnam_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 );
      }
   }
}

请参阅See also

流 I/OStream I/O
_getmbcp_getmbcp
mallocmalloc
_setmbcp_setmbcp
tmpfile_stmpfile_s