std::filesystem::hard_link_count
来自cppreference.com
< cpp | filesystem
| 在标头 <filesystem> 定义
|
||
| |
(1) | (C++17 起) |
| |
(2) | (C++17 起) |
返回路径 p 所标识的文件系统对象的硬链接数。
无抛出重载在错误时返回 static_cast<uintmax_t>(-1)。
参数
| p | - | 要检测的路径 |
| ec | - | 无抛出重载中报告错误的输出形参 |
返回值
p 的硬链接数
异常
若内存分配失败,则任何不标记为 noexcept 的重载可能抛出 std::bad_alloc 。
1) 抛出 std::filesystem::filesystem_error,构造时以
p 为第一路径实参并以OS 错误码为错误码实参。示例
Run this code
#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
参阅
(C++17) |
创建一个硬链接 (函数) |
| 返回指代该 directory_entry 所表示的文件的硬链接数 ( std::filesystem::directory_entry 的公开成员函数)
|