创建临时文件。Creates a temporary file. 此函数已弃用,因为已提供更为安全的版本;请参阅 tmpfile_sThis function is deprecated because a more secure version is available; see tmpfile_s.


FILE *tmpfile( void );

返回值Return Value

如果成功, tmpfile将返回流指针。If successful, tmpfile returns a stream pointer. 否则,它将返回NULL指针。Otherwise, it returns a NULL pointer.


Tmpfile函数创建临时文件并返回指向该流的指针。The tmpfile function creates a temporary file and returns a pointer to that stream. 在根目录中创建了临时文件。The temporary file is created in the root directory. 若要在目录(而非根)中创建临时文件,请将 tmpnamtempnamfopen 结合使用。To create a temporary file in a directory other than the root, use tmpnam or tempnam in conjunction with fopen.

如果文件无法打开,则tmpfile返回NULL指针。If the file cannot be opened, tmpfile returns a NULL pointer. 当文件关闭、程序正常终止时,或在调用 _rmtmp时,如果当前工作目录未更改,则会自动删除此临时文件。This temporary file is automatically deleted when the file is closed, when the program terminates normally, or when _rmtmp is called, assuming that the current working directory does not change. 临时文件以w + b (二进制读取/写入)模式打开。The temporary file is opened in w+b (binary read/write) mode.

如果尝试超过 TMP_MAX,则可能会失败(请参阅 STDIO.H。H)调用tmpfileFailure can occur if you attempt more than TMP_MAX (see STDIO.H) calls with tmpfile.


例程所返回的值Routine 必需的标头Required header
tmpfiletmpfile <stdio.h><stdio.h>

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



此示例需要在 Windows Vista 上运行的管理员特权。This example requires administrative privileges to run on Windows Vista.

// crt_tmpfile.c
// compile with: /W3
// This program uses tmpfile to create a
// temporary file, then deletes this file with _rmtmp.
#include <stdio.h>

int main( void )
   FILE *stream;
   int  i;

   // Create temporary files.
   for( i = 1; i <= 3; i++ )
      if( (stream = tmpfile()) == NULL ) // C4996
      // Note: tmpfile is deprecated; consider using tmpfile_s instead
         perror( "Could not open new temporary file\n" );
         printf( "Temporary file %d was created\n", i );

   // Remove temporary files.
   printf( "%d temporary files deleted\n", _rmtmp() );
Temporary file 1 was created
Temporary file 2 was created
Temporary file 3 was created
3 temporary files deleted

请参阅See also

流 I/OStream I/O
_tempnam、_wtempnam、tmpnam、_wtmpnam_tempnam, _wtempnam, tmpnam, _wtmpnam