命名空间
变体
操作

std::experimental::filesystem::hard_link_count

来自 cppreference.cn
< cpp‎ | experimental‎ | fs
 
 
实验性
技术规范
文件系统库 (文件系统 TS)
库基础 (库基础 TS)
库基础 2 (库基础 TS v2)
库基础 3 (库基础 TS v3)
并行性扩展 (并行性 TS)
并行性扩展 2 (并行性 TS v2)
并发性扩展 (并发性 TS)
并发扩展 2 (并发 TS v2)
概念 (概念 TS)
范围 (范围 TS)
反射 (反射 TS)
数学特殊函数 (特殊函数 TR)
实验性非 TS
模式匹配
线性代数
std::execution
契约
2D 图形
 
 
定义于头文件 <experimental/filesystem>
std::uintmax_t hard_link_count( const path& p );
std::uintmax_t hard_link_count( const path& p, error_code& ec );
(1) (文件系统 TS)

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

不抛出异常的重载在出错时返回 static_cast<uintmax_t>(-1)

目录

[编辑] 参数

p - 要检查的路径
ec - 非抛出重载中用于错误报告的出参

[编辑] 返回值

p 的硬链接数量。

[编辑] 异常

不带 error_code& 参数的重载在底层操作系统 API 错误时抛出 filesystem_error,该异常以 p 作为第一个参数,操作系统错误码作为错误码参数构造。如果内存分配失败,可能会抛出 std::bad_alloc。带有 error_code& 参数的重载,如果操作系统 API 调用失败,则将其设置为操作系统 API 错误码;如果没有发生错误,则执行 ec.clear()。此重载具有
noexcept 规范:  
noexcept
  

[编辑] 示例

#include <experimental/filesystem>
#include <iostream>
namespace fs = std::experimental::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

[编辑] 参见

创建硬链接
(函数) [编辑]