std::filesystem::hard_link_count

Header: <filesystem>

Returns the number of hard links for the filesystem object identified by path p.

# Declarations

std::uintmax_t hard_link_count( const std::filesystem::path& p );

(since C++17)

std::uintmax_t hard_link_count( const std::filesystem::path& p,
std::error_code& ec ) noexcept;

(since C++17)

# Parameters

# Return value

The number of hard links for p.

# Example

#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';
}

# See also