tmpfile

建立暫存檔。 此函式已被取代,因為有更安全的版本可供使用;請參閱 tmpfile_s

語法

FILE *tmpfile( void );

傳回值

如果成功,tmpfile 會傳回資料流指標。 否則會傳回 NULL 指標。

備註

tmpfile 函式會建立暫存檔案,並傳回該資料流的指標。 暫存檔案建立於根目錄。 若要在根目錄以外的目錄中建立暫存檔,請使用 或 tempnam 搭配 fopen 使用 tmpnam

如果無法開啟檔案, 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, _wtempnam, tmpnam, _wtmpnam