tmpfile_s

建立暫存檔。 其版本 tmpfile 具有 CRT 中的安全性功能中所述 的安全性增強功能。

語法

errno_t tmpfile_s(
   FILE** pFilePtr
);

參數

pFilePtr
用來儲存所產生資料流指標位址的指標位址。

傳回值

如果成功,則傳回 0,如果失敗,則為錯誤碼。

錯誤條件

pFilePtr 傳回值 pFilePtr 的內容。
NULL EINVAL 未變更

如果發生上述參數驗證錯誤,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行, errno 則會設定為 EINVAL ,而傳回值為 EINVAL

備註

tmpfile_s 函式會建立暫存檔案,並將該資料流的指標放在 pFilePtr 引數中。 暫存檔案建立於根目錄。 若要在根目錄以外的目錄中建立暫存檔,請使用 或 tempnam 搭配 fopen 使用 tmpnam_s

如果無法開啟檔案, tmpfile_sNULLpFilePtr 寫入 參數。 當檔案關閉、程式正常終止或 _rmtmp 呼叫時,假設目前的工作目錄不會變更,則會自動刪除此暫存檔案。 暫存檔會以 w+b (二進位讀取/寫入)模式開啟。

如果您嘗試的次數超過 TMP_MAX_S ,可能會發生失敗(請參閱 STDIO。H) 使用 tmpfile_s 呼叫。

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
tmpfile_s <stdio.h>

如需相容性詳細資訊,請參閱相容性

範例

注意

此範例可能需要在 Windows 上執行系統管理許可權。

// crt_tmpfile_s.c
// This program uses tmpfile_s to create a
// temporary file, then deletes this file with _rmtmp.
//

#include <stdio.h>

int main( void )
{
   FILE *stream;
   char tempstring[] = "String to be written";
   int  i;
   errno_t err;

   // Create temporary files.
   for( i = 1; i <= 3; i++ )
   {
      err = tmpfile_s(&stream);
      if( err )
         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