_tempnam, _wtempnam, tmpnam, _wtmpnam

產生可用來建立暫存檔的名稱。 這些函式中有一些更安全的版本可供使用;請參閱 tmpnam_s_wtmpnam_s

語法

char *_tempnam(
   const char *dir,
   const char *prefix
);
wchar_t *_wtempnam(
   const wchar_t *dir,
   const wchar_t *prefix
);
char *tmpnam(
   char *str
);
wchar_t *_wtmpnam(
   wchar_t *str
);

參數

prefix
_tempnam前面加上 的名稱字串。

dir
如果沒有 TMP 環境變數,或 TMP 不是有效的目錄,則檔案名中使用的路徑。

str
保留所產生名稱的指標,與函式所傳回的名稱相同。 這是儲存所產生名稱的便利方式。

傳回值

所有這些函式都會傳回所產生名稱的指標,如果 NULL 發生失敗,則傳回 。 如果您嘗試的次數超過 TMP_MAX ,可能會發生失敗(請參閱 STDIO。H) 會使用 tmpnam 或 呼叫 , _tempnam 並在環境變數和 參數中 TMP 指定了不正確 dir 目錄名稱。

注意

tmpnam_wtmpnam 所傳回的指標指向內部靜態緩衝區。 free 不應該呼叫 來解除配置這些指標。 需要針對 _tempnam_wtempnam 配置的指標呼叫 free

備註

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

對於 tmpnam,您可以將這個產生的檔案名稱儲存在 str。 如果 strNULL,則 tmpnam 會將結果保持在內部靜態緩衝區中。 因此任何後續呼叫會終結這個值。 tmpnam 所產生的名稱包含程式所產生的檔案名稱,以及在第一次呼叫 tmpnam 之後,以 32 為基底的序號副檔名 (.1-.vvu,當 TMP_MAX 在 STDIO.H 中時是 32,767)。

_tempnam 為下列規則選擇的目錄產生唯一的檔案名:

  • 如果 TMP 環境變數已定義並設定為有效的目錄名稱,則會為 TMP 所指定的目錄產生唯一檔案名。

  • 如果未定義 TMP 環境變數,或設定為不存在之目錄的名稱, _tempnam 請使用 dir 參數作為它產生唯一名稱的路徑。

  • 如果未定義 TMP 環境變數,或將它設定為不存在的目錄名稱,如果 dirNULL 或 設定為不存在的目錄名稱, _tempnam 請使用目前的工作目錄來產生唯一的名稱。 目前,如果 TMP 和 dir 指定不存在的目錄名稱, 則_tempnam 函式呼叫會失敗。

_tempnam 回的名稱是 的 prefix 串連和序號,結合以建立指定目錄的唯一檔案名。 _tempnam 會產生沒有副檔名的檔案名稱。 _tempnam 會使用 malloc 來設定檔名的空間;當不再需要此空間時,程式會負責釋放此空間。

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

如果 _DEBUG 已定義 和 _CRTDBG_MAP_ALLOC_tempnam 而且 _wtempnam 會由 對 和 _wtempnam_dbg_tempnam_dbg 呼叫取代。

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_ttmpnam tmpnam tmpnam _wtmpnam
_ttempnam _tempnam _tempnam _wtempnam

需求

常式 必要的標頭
_tempnam <stdio.h>
_wtempnam, _wtmpnam <stdio.h > 或 < wchar.h>
tmpnam <stdio.h>

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

範例

// crt_tempnam.c
// compile with: /W3
// This program uses tmpnam to create a unique filename in the
// temporary directory, and _tempname to create a unique filename
// in C:\\tmp.

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

int main(void)
{
   char * name1 = NULL;
   char * name2 = NULL;
   char * name3 = NULL;

   // Create a temporary filename for the current working directory:
   if ((name1 = tmpnam(NULL)) != NULL) { // C4996
   // Note: tmpnam is deprecated; consider using tmpnam_s instead
      printf("%s is safe to use as a temporary file.\n", name1);
   } else {
      printf("Cannot create a unique filename\n");
   }

   // Create a temporary filename in temporary directory with the
   // prefix "stq". The actual destination directory may vary
   // depending on the state of the TMP environment variable and
   // the global variable P_tmpdir.

   if ((name2 = _tempnam("c:\\tmp", "stq")) != NULL) {
      printf("%s is safe to use as a temporary file.\n", name2);
   } else {
      printf("Cannot create a unique filename\n");
   }

   // When name2 is no longer needed:
   if (name2) {
      free(name2);
   }

   // Unset TMP environment variable, then create a temporary filename in C:\tmp.
   if (_putenv("TMP=") != 0) {
      printf("Could not remove TMP environment variable.\n");
   }

   // With TMP unset, we'll use C:\tmp as the temporary directory.
   // Create a temporary filename in C:\tmp with prefix "stq".
   if ((name3 = _tempnam("c:\\tmp", "stq")) != NULL) {
      printf("%s is safe to use as a temporary file.\n", name3);
   }
   else {
      printf("Cannot create a unique filename\n");
   }

   // When name3 is no longer needed:
   if (name3) {
      free(name3);
   }

   return 0;
}
C:\Users\LocalUser\AppData\Local\Temp\sriw.0 is safe to use as a temporary file.
C:\Users\LocalUser\AppData\Local\Temp\stq2 is safe to use as a temporary file.
c:\tmp\stq3 is safe to use as a temporary file.

另請參閱

資料流 I/O
_getmbcp
malloc
_setmbcp
tmpfile
tmpfile_s