Namespaces
Variants

std::filesystem:: relative, std::filesystem:: proximate

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

const std:: filesystem :: path & base,

std:: error_code & ec ) ;
(3) (seit C++17)
path proximate ( const std:: filesystem :: path & p,
std:: error_code & ec ) ;
(4) (seit C++17)
(5) (seit C++17)
path proximate ( const std:: filesystem :: path & p,

const std:: filesystem :: path & base,

std:: error_code & ec ) ;
(6) (seit C++17)
1) Gibt relative ( p, current_path ( ) , ec ) zurück.
2,3) Gibt p relativ zu base zurück. Löst symbolische Links auf und normalisiert sowohl p als auch base vor der weiteren Verarbeitung. Effektiv wird std:: filesystem :: weakly_canonical ( p ) . lexically_relative ( std:: filesystem :: weakly_canonical ( base ) ) oder std:: filesystem :: weakly_canonical ( p, ec ) . lexically_relative ( std:: filesystem :: weakly_canonical ( base, ec ) ) zurückgegeben, außer dass die Error-Code-Variante bei Auftreten eines Fehlers path ( ) zurückgibt.
4) Gibt proximate ( p, current_path ( ) , ec ) zurück.
5,6) Effektiv gibt zurück std:: filesystem :: weakly_canonical ( p ) . lexically_proximate ( std:: filesystem :: weakly_canonical ( base ) ) oder std:: filesystem :: weakly_canonical ( p, ec ) . lexically_proximate ( std:: filesystem :: weakly_canonical ( base, ec ) ) , außer dass die Error-Code-Variante path ( ) beim ersten auftretenden Fehler zurückgibt, falls vorhanden.

Inhaltsverzeichnis

Parameter

p - ein bestehender Pfad
base - Basis-Pfad, relativ zu dem p relativ/proximal gemacht wird
ec - Fehlercode zur Speicherung des Fehlerstatus

Rückgabewert

1) p relativ zu current_path ( ) gemacht.
2,3) p relativ zu base gemacht.
4) p wurde relativ zu current_path ( ) gemacht.
5,6) p in unmittelbare Nähe zu base gebracht.

Exceptions

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

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

Beispiel

#include <filesystem>
#include <iostream>
void show(std::filesystem::path x, std::filesystem::path y)
{
    std::cout << "x:\t\t " << x << "\ny:\t\t " << y << '\n'
              << "relative(x, y):  "
              << std::filesystem::relative(x, y) << '\n'
              << "proximate(x, y): "
              << std::filesystem::proximate(x, y) << "\n\n";
}
int main()
{
    show("/a/b/c", "/a/b");
    show("/a/c", "/a/b");
    show("c", "/a/b");
    show("/a/b", "c");
}

Mögliche Ausgabe:

x:               "/a/b/c"
y:               "/a/b"
relative(x, y):  "c"
proximate(x, y): "c"
x:               "/a/c"
y:               "/a/b"
relative(x, y):  "../c"
proximate(x, y): "../c"
x:               "c"
y:               "/a/b"
relative(x, y):  ""
proximate(x, y): "c"
x:               "/a/b"
y:               "c"
relative(x, y):  ""
proximate(x, y): "/a/b"

Siehe auch

(C++17)
repräsentiert einen Pfad
(Klasse)
(C++17)
erstellt einen absoluten Pfad
(Funktion)
erstellt einen kanonischen Pfad
(Funktion)
konvertiert Pfad in Normalform
konvertiert Pfad in relative Form
konvertiert Pfad in proximale Form
(öffentliche Elementfunktion von std::filesystem::path )