tmpnam_s, _wtmpnam_s

產生可用來建立暫存檔的名稱。 這些函式是 和 的版本 tmpnam_wtmpnam 具有 CRT 中安全性功能中所述 的安全性增強功能。

語法

errno_t tmpnam_s(
   char * str,
   size_t sizeInChars
);
errno_t _wtmpnam_s(
   wchar_t *str,
   size_t sizeInChars
);
template <size_t size>
errno_t tmpnam_s(
   char (&str)[size]
); // C++ only
template <size_t size>
errno_t _wtmpnam_s(
   wchar_t (&str)[size]
); // C++ only

參數

str
[out]保存所產生名稱的指標。

sizeInChars
[in]以字元為單位的緩衝區大小。

傳回值

這些函式成功時均會傳回 0,或在失敗時傳回錯誤號碼。

錯誤條件

str sizeInChars 傳回值 str 的內容。
NULL 任意 EINVAL 未修改
NULL (指向有效的記憶體) 太短 ERANGE 未修改

如果 strNULL ,則會叫用不正確參數處理常式,如參數驗證 中所述 。 如果允許繼續執行,這些函式會將 errno 設為 EINVAL,並傳回 EINVAL

備註

每個函式都會傳回目前不存在的檔案名。 tmpnam_s 會傳回 所 GetTempPathW 傳回之指定 Windows 臨時目錄中的唯一名稱。 當檔案名前面加上反斜線且沒有路徑資訊時,例如 \fname21 ,表示名稱對目前工作目錄有效。

對於 tmpnam_s,您可以將這個產生的檔案名稱儲存在 strtmpnam_s 所傳回的字串最大長度是 L_tmpnam_s,如 STDIO.H 中所定義。 如果 strNULL,則 tmpnam_s 會將結果保持在內部靜態緩衝區中。 因此任何後續呼叫會終結這個值。 所產生的 tmpnam_s 名稱是由程式產生的檔案名所組成,而且在第一次呼叫 tmpnam_s 之後,在 STDIO 中為基底 32 (.1-.1vvvu) TMP_MAX_S 中的序號副檔名。H 為 INT_MAX

tmpnam_s 會自動適當地處理多位元組字元字串引數,並根據從作業系統取得的 OEM 字碼頁辨識多位元組字元序列。 _wtmpnam_stmpnam_s 的寬字元版本,_wtmpnam_s 的引數與傳回值是寬字元字串。 _wtmpnam_stmpnam_s 的行為相同,不同之處在于 _wtmpnam_s 不會處理多位元組字元字串。

在 C++ 中,使用這些函式已透過範本多載簡化;多載可自動推斷緩衝區長度,因而不需要指定大小引數。 如需詳細資訊,請參閱 保護範本多載

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

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_ttmpnam_s tmpnam_s tmpnam_s _wtmpnam_s

需求

常式 必要的標頭
tmpnam_s <stdio.h>
_wtmpnam_s <stdio.h > 或 < wchar.h>

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

範例

// crt_tmpnam_s.c
// This program uses tmpnam_s to create a unique filename in the
// current working directory.
//

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
   char name1[L_tmpnam_s];
   errno_t err;
   int i;

   for (i = 0; i < 15; i++)
   {
      err = tmpnam_s( name1, L_tmpnam_s );
      if (err)
      {
         printf("Error occurred creating unique filename.\n");
         exit(1);
      }
      else
      {
         printf( "%s is safe to use as a temporary file.\n", name1 );
      }
   }
}
C:\Users\LocalUser\AppData\Local\Temp\u19q8.0 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.1 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.2 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.3 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.4 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.5 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.6 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.7 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.8 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.9 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.a is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.b is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.c is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.d is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\u19q8.e is safe to use as a temporary file.

另請參閱

資料流 I/O
_getmbcp
malloc
_setmbcp
tmpfile_s