_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
将预先挂起到 _tempnam返回的名称的字符串。The string that will be pre-pended to names returned by _tempnam.

目录dir
文件名中使用的路径(如果不存在 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

其中每个函数均返回一个指向生成的名称的指针,如果出现错误,则返回NULLEach of these functions returns a pointer to the name generated or NULL if there is a failure. 如果尝试超过TMP_MAX ,则可能会失败(请参阅 stdio.h。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_wtempnam所分配的指针调用freefree 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返回由GetTempPathW返回的指定 Windows 临时目录中唯一的名称。tmpnam 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,你可以将生成的文件名存储在str中。For 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-,TMP_MAX 中的stdio.h时,为的文件扩展名。H 为32767)。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或设置为不存在的目录的名称,则 _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 和目录均指定不存在的目录的名称,则 _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为文件名分配空间;程序负责在不再需要此空间时将其释放。_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. _wtmpnamtmpnam的宽字符版本; _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