_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返回在当前工作目录中唯一的名称和 _tempnam允许您在当前之外的目录中生成一个唯一的名称。tmpnam returns a name unique in the current working directory and _tempnam lets you generate a unique name in a directory other than the current 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 环境变量或如果设置为的名称的目录中不存在,并且如果dirNULL或将设置为不存在,目录的名称 _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文件名; 的分配空间的程序负责在不再需要时释放此空间。_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
// current working directory, then uses _tempnam to create
// a unique filename with a prefix of stq.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char* name1 = NULL;
   char* name2 = 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);

}
\s1gk. is safe to use as a temporary file.
C:\DOCUME~1\user\LOCALS~1\Temp\2\stq2 is safe to use as a temporary file.

请参阅See also

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