名稱空間
變體
操作

std::filesystem::temp_directory_path

來自 cppreference.com
 
 
 
定義於標頭檔案 <filesystem>
path temp_directory_path();
(1) (C++17 起)
path temp_directory_path( std::error_code& ec );
(2) (C++17 起)

返回適合臨時檔案的目錄位置。

目錄

[編輯] 引數

(無)

[編輯] 返回值

適合臨時檔案的目錄。該路徑保證存在且是一個目錄。接受 error_code& 引數的過載在出錯時返回空路徑。

[編輯] 異常

任何未標記為 noexcept 的過載都可能在記憶體分配失敗時丟擲 std::bad_alloc

1) 在底層作業系統 API 錯誤時丟擲 std::filesystem::filesystem_error,構造時以 path to be returned 作為第一個路徑引數,以作業系統錯誤碼作為錯誤碼引數。
2) 如果作業系統 API 呼叫失敗,則將 std::error_code& 引數設定為作業系統 API 錯誤碼;如果沒有錯誤發生,則執行 ec.clear()

[編輯] 注意

在 POSIX 系統上,該路徑可以是環境變數 TMPDIRTMPTEMPTEMPDIR 中指定的路徑,如果這些環境變數都沒有指定,則返回路徑 "/tmp"

在 Windows 系統上,該路徑通常是 GetTempPath 返回的路徑。

[編輯] 示例

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
 
int main()
{
    std::cout << "Temp directory is " << fs::temp_directory_path() << '\n';
}

可能的輸出

Temp directory is "C:\Windows\TEMP\"

[編輯] 參閱

建立並開啟一個臨時檔案,該檔案將自動刪除
(函式) [編輯]
返回或設定當前工作目錄
(函式) [編輯]