Namespaces
Variants

std::filesystem:: hard_link_count

From cppreference.net
定义于头文件 <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()
{
    // 在POSIX风格的文件系统中,每个目录至少包含2个硬链接:
    // 自身和特殊成员路径名 "."
    fs::path p = fs::current_path();
    std::cout << "当前路径的硬链接数量为 "
              << fs::hard_link_count(p) << '\n';
    // 每个 ".." 都是指向父目录的硬链接,因此
    // 任何目录的硬链接总数等于2加上直接子目录的数量
    p = fs::current_path() / ".."; // 每个点-点都是指向父目录的硬链接
    std::cout << ".. 的硬链接数量为 "
              << fs::hard_link_count(p) << '\n';
}

可能的输出:

当前路径的硬链接数量为 2
.. 的硬链接数量为 3

参见

创建硬链接
(函数)
返回指向目录条目所引用文件的硬链接数量
( std::filesystem::directory_entry 的公开成员函数)