tmpfile

创建临时文件。 此函数已弃用,因为已提供更为安全的版本;请参阅tmpfile_s

语法

FILE *tmpfile( void );

返回值

如果成功,则 tmpfile 返回流指针。 否则,返回 NULL 指针。

备注

tmpfile 函数创建临时文件并返回指向该流的指针。 在根目录中创建了临时文件。 若要在根目录以外的目录中创建临时文件,请结合使用 tmpnamtempnamfopen

如果文件无法打开,tmpfile 将返回 NULL 指针。 当文件关闭、程序正常终止或调用 _rmtmp 时,此临时文件将被自动删除(假定当前工作目录未更改)。 临时文件在 w+b(二进制读/写)模式下打开。

如果尝试使用 tmpfile 执行超过 TMP_MAX 次调用(请参阅 STDIO.H),则可能失败。

要求

例程 必需的标头
tmpfile <stdio.h>

有关兼容性的详细信息,请参阅 兼容性

示例

注意

此示例需要在 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" );
      else
         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

另请参阅

流 I/O
_rmtmp
_tempnam_wtempnamtmpnam_wtmpnam