std::filesystem::hard_link_count
来自 cppreference.com
< cpp | filesystem
在头文件 <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。
[编辑] 示例
运行此代码
#include <filesystem> #include <iostream> namespace fs = std::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
[编辑] 另请参阅
(C++17) |
创建硬链接 (函数) |
返回指向目录项所指向文件的硬链接数 ( std::filesystem::directory_entry 的公共成员函数) |