名稱空間
變體
操作

std::filesystem::hard_link_count

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

返回由路徑 p 標識的檔案系統物件的硬連結數量。

無丟擲異常的過載在錯誤發生時返回 static_cast<uintmax_t>(-1)

目錄

[編輯] 引數

p - 要檢查的路徑
ec - 非丟擲過載中用於錯誤報告的出參

[編輯] 返回值

路徑 p 的硬連結數量。

[編輯] 異常

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

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

[編輯] 示例

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
 
int main()
{
    // On a POSIX-style filesystem, each directory has at least 2 hard links:
    // itself and the special member pathname "."
    fs::path p = fs::current_path();
    std::cout << "Number of hard links for current path is "
              << fs::hard_link_count(p) << '\n';
 
    // Each ".." is a hard link to the parent directory, so the total number
    // of hard links for any directory is 2 plus number of direct subdirectories
    p = fs::current_path() / ".."; // Each dot-dot is a hard link to parent
    std::cout << "Number of hard links for .. is "
              << fs::hard_link_count(p) << '\n';
}

可能的輸出

Number of hard links for current path is 2
Number of hard links for .. is 3

[編輯] 參閱

建立硬連結
(函式) [編輯]
返回目錄項所引用檔案的硬連結數量
(std::filesystem::directory_entry 的公開成員函式) [編輯]