名稱空間
變體
操作

std::filesystem::directory_entry::replace_filename

來自 cppreference.com
 
 
 
 
void replace_filename( const std::filesystem::path& p );
(1) (C++17 起)
void replace_filename( const std::filesystem::path& p, std::error_code& ec );
(2) (C++17 起)

修改目錄項的檔名。

透過 path.replace_filename(p) 有效地修改路徑成員,並呼叫 refresh 以更新快取屬性。如果發生錯誤,快取屬性的值將是不確定的。

此函式不會對檔案系統提交任何更改。

目錄

[編輯] 引數

p - 要附加到當前儲存路徑的父路徑的路徑
ec - 非丟擲過載中用於錯誤報告的出參

[編輯] 返回值

(無)

[編輯] 異常

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

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

[編輯] 示例

#include <filesystem>
#include <iostream>
 
int main()
{
    namespace fs = std::filesystem;
    {
        fs::directory_entry entry{"alpha"};
        std::cout << entry << '\n';
        entry.replace_filename("omega");
        std::cout << entry << '\n';
    }
    {
        fs::directory_entry entry{"/alpha/"};
        std::cout << entry << '\n';
        entry.replace_filename("omega");
        std::cout << entry << '\n';
    }
}

輸出

"alpha"
"omega"
"/alpha/"
"/alpha/omega"

[編輯] 參閱

賦值內容
(公共成員函式) [編輯]
用另一個路徑替換最後一個路徑元件
(std::filesystem::path 的公共成員函式) [編輯]