名前空間
変種
操作

std::filesystem::hard_link_count

From cppreference.com
 
 
 
ヘッダー <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 - 検査するパス
エラーコード - 例外を投げないオーバーロードでのエラー報告のための出力パラメータ

[編集] 戻り値

p のハードリンク数。

[編集] 例外

noexcept とマークされていないオーバーロードは、メモリ割り当てが失敗した場合に std::bad_alloc をスローする可能性があります。

1) 基盤となるOS APIエラーが発生した場合、最初のパス引数としてp、エラーコード引数としてOSのエラーコードとともに構築されたstd::filesystem::filesystem_error を投げます。
2) OS API呼び出しが失敗した場合、std::error_code& パラメータにOS APIのエラーコードが設定されます。エラーが発生しなかった場合は、ec.clear() が実行されます。

[編集]

#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

[編集] 関連項目

ハードリンクを作成する
(関数) [編集]
ディレクトリエントリが参照するファイルを参照するハードリンクの数を返します
(std::filesystem::directory_entry の public メンバ関数) [編集]
English 日本語 中文(简体) 中文(繁體)