Namespaces
Variants

std::filesystem:: hard_link_count

From cppreference.net
Definiert im Header <filesystem>
std:: uintmax_t hard_link_count ( const std:: filesystem :: path & p ) ;
(1) (seit C++17)
std:: uintmax_t hard_link_count ( const std:: filesystem :: path & p,
std:: error_code & ec ) noexcept ;
(2) (seit C++17)

Gibt die Anzahl der festen Verknüpfungen für das Dateisystemobjekt zurück, das durch den Pfad p identifiziert wird.

Die nicht-werfende Überladung gibt bei Fehlern static_cast < uintmax_t > ( - 1 ) zurück.

Inhaltsverzeichnis

Parameter

p - zu untersuchender Pfad
ec - Out-Parameter für Fehlerberichterstattung in der nicht-werfenden Überladung

Rückgabewert

Die Anzahl der Hardlinks für p .

Exceptions

Jede Überladung, die nicht als noexcept gekennzeichnet ist, kann std::bad_alloc auslösen, wenn die Speicherallokation fehlschlägt.

1) Wirft std::filesystem::filesystem_error bei zugrundeliegenden OS-API-Fehlern, konstruiert mit p als erstem Pfadargument und dem OS-Fehlercode als Fehlercodeargument.
2) Setzt einen std:: error_code & Parameter auf den OS-API-Fehlercode, wenn ein OS-API-Aufruf fehlschlägt, und führt ec. clear ( ) aus, falls keine Fehler auftreten.

Beispiel

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

Mögliche Ausgabe:

Number of hard links for current path is 2
Number of hard links for .. is 3

Siehe auch

Erstellt eine Hardlink
(Funktion)
Gibt die Anzahl der Hardlinks zurück, die auf die Datei verweisen, auf die der Directory-Eintrag verweist
(Öffentliche Mitgliedsfunktion von std::filesystem::directory_entry )