命名空间
变体
操作

std::filesystem::hard_link_count

来自 cppreference.cn
 
 
 
定义于头文件 <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 的公开成员函数) [编辑]