std::filesystem::hard_link_count

定义于头文件 <filesystem>
std::uintmax_t hard_link_count( const std::filesystem::path& p );
std::uintmax_t hard_link_count( const std::filesystem::path& p, std::error_code& ec );
(1) (C++17 起)

返回路径 p 所标识的文件系统对象的硬链接数。

非抛出重载在错误时返回 static_cast<uintmax_t>(-1)

目录

参数

p - 要检测的路径
ec - 非抛出重载中报告错误的输出参数

返回值

p 的硬链接数

异常

不接受 std::error_code& 参数的重载在底层 OS API 错误时抛出 filesystem_error ,以第一参数 p 和作为错误码参数的 OS 错误码构造。若内存分配失败则可能抛出 std::bad_alloc 。若 OS API 调用失败,则接受 std::error_code& 参数的重载设置该参数为 OS API 错误码,而若不出现错误则执行 ec.clear()

示例

#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
    // 在 POSIX 风格文件系统上,每个目录至少有 2 个硬链接:
    // 其自身与特殊成员名 "."
    fs::path p = fs::current_path();
    std::cout << "Number of hard links for current path is "
              << fs::hard_link_count(p) << '\n';
 
    // 每个 ".." 是到亲目录的应谅解,故对任何目录的硬链接综述是 2 加上直接子目录数
    p = fs::current_path() / ".."; // 每个双点是到亲目录的硬链接
    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

参阅

创建一个硬链接
(函数)
返回引用到该 directory_entry 所表示的文件的硬链接数
(std::filesystem::directory_entry 的公开成员函数)

版本历史

  • (当前 | 先前 2017年4月30日 (日) 01:02Fruderica讨论 | 贡献 . . (1,629字节) (-1). . (撤销)
  • 当前 | 先前) 2017年4月30日 (日) 01:00Fruderica讨论 | 贡献. . (1,630字节) (+1,630). . (以“{{cpp/filesystem/title|hard_link_count}} {{cpp/filesystem/navbar}} {{dcl begin}} {{dcl header | filesystem}} {{dcl | num=1 | since=c++17 | 1= std::uintmax_t hard_lin...”为内容创建页面)