directory_entry 類別

描述一個物件,透過取消參照 a directory_iterator 或 a recursive_directory_iterator返回。 A directory_entry 會包裝 a path ,並且作為優化,可以快取在迭代目錄時取得的檔案屬性。

語法

class directory_entry;

備註

物件 directory_entry 儲存一個 path。 作為優化,它也能快取路徑所參考檔案的屬性與狀態。 快取資料會在目錄迭代器建立條目時,或你呼叫 refresh時填充。 觀察者如 file_size、 last_write_time、 status及各種 is_* 謂詞在快取資料可用時回傳;否則,他們會查詢檔案系統。

如需詳細資訊與程式碼範例,請參閱檔案系統巡覽 (C++)。

建構函式

建構函式 描述
directory_entry 建構一個 directory_entry。

成員函式

成員函數 描述
assign 替換儲存的路徑並刷新快取的屬性。
exists 檢查該條目是否指向現有檔案。
file_size 取得參考檔案的大小(位元組)。
hard_link_count 會取得指向該檔案的硬連結數量。
is_block_file 檢查所參考檔案是否為區塊特殊檔案。
is_character_file 檢查所參考的檔案是否為字元特殊檔案。
is_directory 檢查所參考的檔案是否為目錄。
is_fifo 檢查所參考檔案是否為命名管道(FIFO)。
is_other 檢查被參考的檔案是否為其他檔案。
is_regular_file 檢查所參考的檔案是否為一般檔案。
is_socket 檢查所參考的檔案是否為套接字。
is_symlink 檢查所參考的檔案是否為符號連結。
last_write_time 取得參考檔案最後一次資料修改的時間。
path 回傳儲存的路徑。
refresh 刷新快取的檔案屬性。
replace_filename 替換儲存路徑的檔名並刷新快取屬性。
status 透過符號連結取得參考檔案的狀態。
symlink_status 取得參考檔案的狀態,無需追蹤符號連結。

操作員

運算子 描述
operator= 指派到 directory_entry。
operator const path& 回傳儲存的路徑。
operator== 檢查兩個 directory_entry 物體是否相等。
operator<=> 對兩個 directory_entry 物件進行三向比較。 (C++20)
operator!= 檢查兩個 directory_entry 物體是否不相等。 C++17 中的明確成員;透過 C++20 及更新版本的重寫比較來支援。
operator< 檢查排序是否 directory_entry 先於另一個 directory_entry。 C++17 中的明確成員;透過 C++20 及更新版本的重寫比較來支援。
operator<= 檢查排序是否 directory_entry 等於另一個 directory_entry。 C++17 中的明確成員;透過 C++20 及更新版本的重寫比較來支援。
operator> 檢查排序是否 directory_entry 接著 directory_entry。 C++17 中的明確成員;透過 C++20 及更新版本的重寫比較來支援。
operator>= 檢查排序是否 directory_entry 等於另一個 directory_entry。 C++17 中的明確成員;透過 C++20 及更新版本的重寫比較來支援。

需求

標頭:<filesystem>

命名空間:std::filesystem

directory_entry

建構一個 directory_entry。

directory_entry() noexcept = default;
directory_entry(const directory_entry&) = default;
directory_entry(directory_entry&&) noexcept = default;

explicit directory_entry(const std::filesystem::path& p);
directory_entry(const std::filesystem::path& p, std::error_code& ec);

~directory_entry();

參數

p
條目所指向的檔案路徑。

電子商務
操作的輸出錯誤碼。

備註

預設、複製與移動構造子的行為皆如預期般運作。 這些構造子會先儲存 pathp ,然後呼叫 refresh 來填充快取的屬性。 過 error_code 載會以 ec 報告錯誤而非拋出,若刷新失敗則清除儲存路徑。

operator=

指派到 directory_entry。

directory_entry& operator=(const directory_entry&) = default;
directory_entry& operator=(directory_entry&&) noexcept = default;

參數

right
directory_entry複製或進入這個directory_entry。

備註

預設的指派運算子行為如預期般。

指派

替換儲存的路徑並刷新快取的屬性。

void assign(const std::filesystem::path& p);
void assign(const std::filesystem::path& p, std::error_code& ec);

參數

p
新的儲存路徑。

電子商務
操作的輸出錯誤碼。

備註

將儲存的路徑替換為 p,然後呼叫 refresh 以更新快取屬性。 超載時會 error_code 報告 錯誤,而不是 丟棄。

replace_filename

替換儲存路徑的檔名並刷新快取屬性。

void replace_filename(const std::filesystem::path& p);
void replace_filename(const std::filesystem::path& p, std::error_code& ec);

參數

p
替換檔名。

電子商務
操作的輸出錯誤碼。

備註

將儲存路徑的檔名部分替換為 p,如同由 , path().replace_filename(p)然後呼叫 refresh。 超載時會 error_code 報告 錯誤,而不是 丟棄。

重新整理

刷新快取的檔案屬性。

void refresh();
void refresh(std::error_code& ec) noexcept;

參數

電子商務
操作的輸出錯誤碼。

備註

讀取儲存路徑所指的檔案屬性,並將其快取於 directory_entry。 在參考檔案變更後呼叫 refresh 以更新快取資料。 超載時會 error_code 報告 錯誤,而不是 丟棄。

路徑

回傳儲存的路徑。

const std::filesystem::path& path() const noexcept;

運算子 const path&

回傳儲存的路徑。

operator const std::filesystem::path&() const noexcept;

存在

檢查該條目是否指向現有檔案。

bool exists() const;
bool exists(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果儲存路徑指向已存在的檔案;否則,。 false 等同於呼叫 filesystem::exists(status())。

is_block_file

檢查所參考檔案是否為區塊特殊檔案。

bool is_block_file() const;
bool is_block_file(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是區塊特殊檔案;否則,。 false 這個功能在 Windows 上總是會回來false。

is_character_file

檢查所參考的檔案是否為字元特殊檔案。

bool is_character_file() const;
bool is_character_file(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是字元特殊檔案;否則,。 false 這個功能在 Windows 上總是會回來false。

is_directory

檢查所參考的檔案是否為目錄。

bool is_directory() const;
bool is_directory(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是目錄;否則,。 false 等同於呼叫 filesystem::is_directory(status())。

is_fifo

檢查所參考檔案是否為命名管道(FIFO)。

bool is_fifo() const;
bool is_fifo(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是 FIFO;否則,。 false 這個功能在 Windows 上總是會回來false。

is_other

檢查被參考的檔案是否為其他檔案。

bool is_other() const;
bool is_other(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考檔案存在但不是一般檔案、目錄或符號連結;否則,。 false 等同於呼叫 filesystem::is_other(status())。

is_regular_file

檢查所參考的檔案是否為一般檔案。

bool is_regular_file() const;
bool is_regular_file(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是一般檔案;否則,。 false 等同於呼叫 filesystem::is_regular_file(status())。

is_socket

檢查所參考的檔案是否為套接字。

bool is_socket() const;
bool is_socket(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是套接字;否則,。 false 這個功能在 Windows 上總是會回來false。

檢查所參考的檔案是否為符號連結。

bool is_symlink() const;
bool is_symlink(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

true如果參考的檔案是符號連結;否則,。 false 等同於呼叫 filesystem::is_symlink(symlink_status())。

檔案大小

取得參考檔案的大小(位元組)。

uintmax_t file_size() const;
uintmax_t file_size(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

參考檔案的大小(以位元組為單位)等,相當於 。filesystem::file_size(path()) 當有快取資料可用時會使用。

會取得指向該檔案的硬連結數量。

uintmax_t hard_link_count() const;
uintmax_t hard_link_count(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

指向參考檔案的硬連結數量,就像 。filesystem::hard_link_count(path()) 當有快取資料可用時會使用。

last_write_time

取得參考檔案最後一次資料修改的時間。

file_time_type last_write_time() const;
file_time_type last_write_time(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

參考檔案最後一次資料修改的時間,就像由 filesystem::last_write_time(path())。 當有快取資料可用時會使用。

狀態

透過符號連結取得參考檔案的狀態。

file_status status() const;
file_status status(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

參考檔案的狀態,就像 。filesystem::status(path()) 當有快取資料可用時會使用。

取得參考檔案的狀態,無需追蹤符號連結。

file_status symlink_status() const;
file_status symlink_status(std::error_code& ec) const noexcept;

參數

電子商務
操作的輸出錯誤碼。

返回值

參考檔案的符號連結狀態,就像 。filesystem::symlink_status(path()) 當有快取資料可用時會使用。

operator==

檢查兩個 directory_entry 物體是否相等。

bool operator==(const directory_entry& right) const noexcept;

參數

right
比較的對象 directory_entry 。

返回值

傳回 path() == right.path()。

operator<=>

對兩個 directory_entry 物件進行三向比較。

std::strong_ordering operator<=>(const directory_entry& right) const noexcept; // C++20

參數

right
比較的對象 directory_entry 。

返回值

傳回 path() <=> right.path()。

備註

支援 C++20 及更新版本。 編譯器會重寫使用 operator<、 operator<=、 operator>或 operator>= 的比較,以使用此運算子。 它會重寫過去 operator!= 使用 operator==的比較。

operator!=

檢查兩個 directory_entry 物體是否不相等。

bool operator!=(const directory_entry& right) const noexcept; // C++17

參數

right
比較的對象 directory_entry 。

返回值

傳回 !(*this == right)。

備註

在 C++17 中,此運算子是明確宣告的成員函式。 在 C++20 及以後版本中,使用 的 operator!= 表達式會被重寫為 operator==。

operator<

檢查排序是否 directory_entry 先於另一個 directory_entry。

bool operator<(const directory_entry& right) const noexcept; // C++17

參數

right
比較的對象 directory_entry 。

返回值

傳回 path() < right.path()。

備註

在 C++17 中,此運算子是明確宣告的成員函式。 在 C++20 及以後版本中,使用 的 operator< 表達式會被重寫為 operator<=>。

operator<=

檢查排序是否 directory_entry 等於另一個 directory_entry。

bool operator<=(const directory_entry& right) const noexcept; // C++17

參數

right
比較的對象 directory_entry 。

返回值

傳回 !(right < *this)。

備註

在 C++17 中,此運算子是明確宣告的成員函式。 在 C++20 及以後版本中,使用 的 operator<= 表達式會被重寫為 operator<=>。

operator>

檢查排序是否 directory_entry 接著 directory_entry。

bool operator>(const directory_entry& right) const noexcept; // C++17

參數

right
比較的對象 directory_entry 。

返回值

傳回 right < *this。

備註

在 C++17 中,此運算子是明確宣告的成員函式。 在 C++20 及以後版本中,使用 的 operator> 表達式會被重寫為 operator<=>。

operator>=

檢查排序是否 directory_entry 等於另一個 directory_entry。

bool operator>=(const directory_entry& right) const noexcept; // C++17

參數

right
比較的對象 directory_entry 。

返回值

傳回 !(*this < right)。

備註

在 C++17 中,此運算子是明確宣告的成員函式。 在 C++20 及以後版本中,使用 的 operator>= 表達式會被重寫為 operator<=>。

另請參閱

標頭檔參考
<filesystem>
檔案系統導航(C++)