命名空间
变体
操作

std::experimental::filesystem::hard_link_count

来自 cppreference.cn
< cpp‎ | experimental‎ | fs
 
 
实验性
技术规范
文件系统库 (filesystem TS)
库基础 (library fundamentals TS)
库基础 2 (library fundamentals TS v2)
库基础 3 (library fundamentals TS v3)
并行性扩展 (parallelism TS)
并行性扩展 2 (parallelism TS v2)
并发性扩展 (concurrency TS)
并发性扩展 2 (concurrency TS v2)
概念 (concepts TS)
范围 (ranges TS)
反射 (reflection TS)
数学特殊函数 (special functions 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) (filesystem 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

[编辑] 参见

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