std::filesystem:: canonical, std::filesystem:: weakly_canonical
|
Definiert im Header
<filesystem>
|
||
|
path canonical
(
const
std::
filesystem
::
path
&
p
)
;
|
(1) | (seit C++17) |
|
path canonical
(
const
std::
filesystem
::
path
&
p,
std:: error_code & ec ) ; |
(2) | (seit C++17) |
|
path weakly_canonical
(
const
std::
filesystem
::
path
&
p
)
;
|
(3) | (seit C++17) |
|
path weakly_canonical
(
const
std::
filesystem
::
path
&
p,
std:: error_code & ec ) ; |
(4) | (seit C++17) |
canonical()
mit einem Pfadargument zusammengesetzt ist, das aus den führenden Elementen von
p
besteht, die existieren (bestimmt durch
status
(
p
)
oder
status
(
p, ec
)
), falls vorhanden, gefolgt von den Elementen von
p
, die nicht existieren. Der resultierende Pfad liegt in
Normalform
.
Inhaltsverzeichnis |
Parameter
| p | - |
ein Pfad, der absolut oder relativ sein kann; für
canonical
muss es ein existierender Pfad sein
|
| ec | - | Fehlercode, in dem der Fehlerstatus gespeichert wird |
Rückgabewert
Ausnahmen
Jede Überladung, die nicht als
noexcept
gekennzeichnet ist, kann
std::bad_alloc
auslösen, wenn die Speicherallokation fehlschlägt.
Hinweise
Die Funktion
canonical()
ist nach dem POSIX-Standard
realpath
modelliert.
Die Funktion
weakly_canonical()
wurde eingeführt, um die operationelle Semantik von
relative()
zu vereinfachen.
Beispiel
#include <filesystem> #include <iostream> int main() { /* Sandbox-Verzeichnisse einrichten: a └── b ├── c1 │ └── d <== aktueller Pfad └── c2 └── e */ auto old = std::filesystem::current_path(); auto tmp = std::filesystem::temp_directory_path(); std::filesystem::current_path(tmp); auto d1 = tmp / "a/b/c1/d"; auto d2 = tmp / "a/b/c2/e"; std::filesystem::create_directories(d1); std::filesystem::create_directories(d2); std::filesystem::current_path(d1); auto p1 = std::filesystem::path("../../c2/./e"); auto p2 = std::filesystem::path("../no-such-file"); std::cout << "Current path is " << std::filesystem::current_path() << '\n' << "Canonical path for " << p1 << " is " << std::filesystem::canonical(p1) << '\n' << "Weakly canonical path for " << p2 << " is " << std::filesystem::weakly_canonical(p2) << '\n'; try { [[maybe_unused]] auto x_x = std::filesystem::canonical(p2); // NICHT ERREICHT } catch (const std::exception& ex) { std::cout << "Canonical path for " << p2 << " threw exception:\n" << ex.what() << '\n'; } // Bereinigung std::filesystem::current_path(old); const auto count = std::filesystem::remove_all(tmp / "a"); std::cout << "Deleted " << count << " files or directories.\n"; }
Mögliche Ausgabe:
Current path is "/tmp/a/b/c1/d" Canonical path for "../../c2/./e" is "/tmp/a/b/c2/e" Weakly canonical path for "../no-such-file" is "/tmp/a/b/c1/no-such-file" Canonical path for "../no-such-file" threw exception: filesystem error: in canonical: No such file or directory [../no-such-file] [/tmp/a/b/c1/d] Deleted 6 files or directories.
Fehlerberichte
Die folgenden verhaltensändernden Fehlerberichte wurden rückwirkend auf zuvor veröffentlichte C++-Standards angewendet.
| DR | Angewendet auf | Verhalten wie veröffentlicht | Korrektes Verhalten |
|---|---|---|---|
| LWG 2956 | C++17 |
canonical
hat einen überflüssigen
base
Parameter
|
entfernt |
Siehe auch
|
(C++17)
|
repräsentiert einen Pfad
(Klasse) |
|
(C++17)
|
erstellt einen absoluten Pfad
(Funktion) |
|
(C++17)
|
erstellt einen relativen Pfad
(Funktion) |