_tempnam、_wtempnam、tmpnam、_wtmpnam_tempnam, _wtempnam, tmpnam, _wtmpnam

生成可用于创建临时文件的名称。Generate names you can use to create temporary files. 提供这些函数的更多安全版本;请参阅 tmpnam_s、_wtmpnam_sMore secure versions of some of these functions are available; see tmpnam_s, _wtmpnam_s.

语法Syntax

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
);

参数Parameters

prefixprefix
将前悬移到返回的名称的字符串 _tempnamThe string that will be pre-pended to names returned by _tempnam.

dirdir
文件名中使用的路径(如果不存在 TMP 环境变量或 TMP 不是有效的目录)。The path used in the file name if there is no TMP environment variable, or if TMP is not a valid directory.

strstr
指针将保留生成的名称,该名称将与函数所返回的名称相同。Pointer that will hold the generated name and will be identical to the name returned by the function. 这是保存生成的名称的简便方法。This is a convenient way to save the generated name.

返回值Return Value

每个函数返回一个指向生成的名称或NULL故障时。Each of these functions returns a pointer to the name generated or NULL if there is a failure. 如果你尝试,则会发生故障超过TMP_MAX (请参阅 STDIO。使用 H) 调用tmpnam或者如果您使用 _tempnam ,并且没有在 TMP 环境变量并在指定了无效的目录名称dir参数。Failure can occur if you attempt more than TMP_MAX (see STDIO.H) calls with tmpnam or if you use _tempnam and there is an invalid directory name specified in the TMP environment variable and in the dir parameter.

备注

通过返回的指针tmpnam_wtmpnam指向内部静态缓冲区。The pointers returned by tmpnam and _wtmpnam point to internal static buffers. 不应调用 free 来释放这些指针。free should not be called to deallocate those pointers. 免费需要通过分配的指针调用 _tempnam_wtempnamfree needs to be called for pointers allocated by _tempnam and _wtempnam.

备注Remarks

这些函数返回的文件名当前不存在。Each of these functions returns the name of a file that does not currently exist. tmpnam返回在指定返回的 Windows 临时目录中是唯一的名称GetTempPathWtmpnam returns a name that's unique in the designated Windows temporary directory returned by GetTempPathW. _tempnam以外指定目录中生成的唯一名称。_tempnam generates a unique name in a directory other than the designated one. 请注意,如果一个文件名称使用反斜杠作为前缀(如 \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,可以存储在此生成的文件名称strFor tmpnam, you can store this generated file name in str. 如果strNULL,然后tmpnam将结果留在内部静态缓冲区中。If str is NULL, then tmpnam leaves the result in an internal static buffer. 因此,任何后续调用都会破坏该值。Thus any subsequent calls destroy this value. 通过生成的名称tmpnam包含的程序生成的文件名称,并在首次调用后tmpnam,基数 32 连续数字的文件扩展名 (.1-.vvu,当TMP_MAX STDIO 中。H 是 32,767)。The name generated by tmpnam consists of a program-generated file name and, after the first call to tmpnam, a file extension of sequential numbers in base 32 (.1-.vvu, when TMP_MAX in STDIO.H is 32,767).

_tempnam将生成唯一的文件名由以下规则为所选目录:_tempnam will generate a unique file name for a directory chosen by the following rules:

  • 如果定义了 TMP 环境变量并将其设置为有效的目录名称,则 TMP 所指定的目录将生成唯一文件名。If the TMP environment variable is defined and set to a valid directory name, unique file names will be generated for the directory specified by TMP.

  • 如果未定义 TMP 环境变量,或如果设置为不存在的目录的名称 _tempnam将使用dir参数,因为它将为其生成的唯一名称的路径。If the TMP environment variable is not defined or if it is set to the name of a directory that does not exist, _tempnam will use the dir parameter as the path for which it will generate unique names.

  • 如果未定义 TMP 环境变量或如果设置为不存在的目录的名称并且如果dir可以是NULL设置为不存在的目录的名称或 _tempnam将使用当前工作目录生成唯一的名称。If the TMP environment variable is not defined or if it is set to the name of a directory that does not exist, and if dir is either NULL or set to the name of a directory that does not exist, _tempnam will use the current working directory to generate unique names. 目前,如果 TMP 和dir指定的目录不存在,名称 _tempnam函数调用将失败。Currently, if both TMP and dir specify names of directories that do not exist, the _tempnam function call will fail.

返回的名称 _tempnam将是一个串连前缀和顺序号的组合到一起以创建唯一的文件名为指定的目录。The name returned by _tempnam will be a concatenation of prefix and a sequential number, which will combine to create a unique file name for the specified directory. _tempnam生成无扩展名的文件名。_tempnam generates file names that have no extension. _tempnam使用malloc为 filename; 分配空间的程序是负责在不再需要时释放此空间。_tempnam uses malloc to allocate space for the filename; the program is responsible for freeing this space when it is no longer needed.

_tempnamtmpnam从操作系统句柄多字节字符字符串参数,根据 OEM 代码页识别多字节字符序列中自动获取。_tempnam and tmpnam automatically handle multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the OEM code page obtained from the operating system. _wtempnam是宽字符版本 _tempnam; 的参数和返回值 _wtempnam都是宽字符字符串。_wtempnam is a wide-character version of _tempnam; the arguments and return value of _wtempnam are wide-character strings. _wtempnam_tempnam行为方式相同,只不过 _wtempnam不处理多字节字符字符串。_wtempnam and _tempnam behave identically except that _wtempnam does not handle multibyte-character strings. _wtmpnam是宽字符版本tmpnam; 的自变量和返回值 _wtmpnam都是宽字符字符串。_wtmpnam is a wide-character version of tmpnam; the argument and return value of _wtmpnam are wide-character strings. _wtmpnamtmpnam行为方式相同,只不过 _wtmpnam不处理多字节字符字符串。_wtmpnam and tmpnam behave identically except that _wtmpnam does not handle multibyte-character strings.

如果 _DEBUG_CRTDBG_MAP_ALLOC定义了, _tempnam_wtempnam对的调用替换为_tempnam_dbg 和 _wtempnam_dbgIf _DEBUG and _CRTDBG_MAP_ALLOC are defined, _tempnam and _wtempnam are replaced by calls to _tempnam_dbg and _wtempnam_dbg.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_ttmpnam_ttmpnam tmpnamtmpnam tmpnamtmpnam _wtmpnam_wtmpnam
_ttempnam_ttempnam _tempnam_tempnam _tempnam_tempnam _wtempnam_wtempnam

要求Requirements

例程所返回的值Routine 必需的标头Required header
_tempnam_tempnam <stdio.h><stdio.h>
_wtempnam_wtmpnam_wtempnam, _wtmpnam <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>
tmpnamtmpnam <stdio.h><stdio.h>

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

示例Example

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

请参阅See also

流 I/OStream I/O
_getmbcp_getmbcp
mallocmalloc
_setmbcp_setmbcp
tmpfiletmpfile
tmpfile_stmpfile_s