_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

prefix
将在由 _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.

str
指针将保留生成的名称,该名称将与函数所返回的名称相同。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) 使用调用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 返回当前工作目录中唯一的名称,而 _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,可以在 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 中的连续数字的文件扩展名(当 STDIO.H 中的 TMP_MAX 为 32,767 时,扩展名为 .1-.vvu)。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 环境变量,或为其设置了不存在的目录名称,则 _tempnamdir 参数用作生成唯一名称的路径。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 返回的名称将是 prefix 和顺序号的串联的组合,为指定目录创建唯一的文件名。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_dbg 和 _wtempnam_dbg 替换 _tempnam_wtempnamIf _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 tmpnam tmpnam _wtmpnam
_ttempnam _tempnam _tempnam _wtempnam

惠?Requirements

例程所返回的值Routine 必需的标头Required header
_tempnam <stdio.h><stdio.h>
_wtempnam, _wtmpnam_wtempnam, _wtmpnam <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>
tmpnam <stdio.h><stdio.h>

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

示例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/O Stream I/O
_getmbcp _getmbcp
malloc malloc
_setmbcp _setmbcp
tmpfile tmpfile
tmpfile_stmpfile_s