1 | // filesystem_path_example.cpp |
---|
2 | // compile by using: /EHsc |
---|
3 | #include <string> |
---|
4 | #include <iostream> |
---|
5 | #include <sstream> |
---|
6 | #include <filesystem> |
---|
7 | |
---|
8 | using namespace std; |
---|
9 | namespace fs = std::filesystem; |
---|
10 | |
---|
11 | // example adapted from https://docs.microsoft.com/pl-pl/cpp/standard-library/file-system-navigation |
---|
12 | void DisplayPathInfo(const fs::path& pathToShow) |
---|
13 | { |
---|
14 | int i = 0; |
---|
15 | cout << "Displaying path info for: " << pathToShow << "\n"; |
---|
16 | for (const auto& part : pathToShow) |
---|
17 | { |
---|
18 | cout << "path part: " << i++ << " = " << part << "\n"; |
---|
19 | } |
---|
20 | |
---|
21 | cout << "exists() = " << fs::exists(pathToShow) << "\n" |
---|
22 | << "root_name() = " << pathToShow.root_name() << "\n" |
---|
23 | << "root_path() = " << pathToShow.root_path() << "\n" |
---|
24 | << "relative_path() = " << pathToShow.relative_path() << "\n" |
---|
25 | << "parent_path() = " << pathToShow.parent_path() << "\n" |
---|
26 | << "filename() = " << pathToShow.filename() << "\n" |
---|
27 | << "stem() = " << pathToShow.stem() << "\n" |
---|
28 | << "extension() = " << pathToShow.extension() << "\n"; |
---|
29 | |
---|
30 | try |
---|
31 | { |
---|
32 | cout << "canonical() = " << fs::canonical(pathToShow) << "\n"; |
---|
33 | } |
---|
34 | catch (fs::filesystem_error err) |
---|
35 | { |
---|
36 | cout << "exception: " << err.what() << "\n"; |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | int main(int argc, char* argv[]) |
---|
42 | { |
---|
43 | const fs::path pathToShow{ argc >= 2 ? argv[1] : fs::current_path() }; |
---|
44 | |
---|
45 | DisplayPathInfo(pathToShow); |
---|
46 | |
---|
47 | cout << "path concat/append:\n"; |
---|
48 | fs::path p1("C:\\temp"); |
---|
49 | p1 /= "user"; |
---|
50 | p1 /= "data"; |
---|
51 | cout << p1 << "\n"; |
---|
52 | |
---|
53 | fs::path p2("C:\\temp\\"); |
---|
54 | p2 += "user"; |
---|
55 | p2 += "data"; |
---|
56 | cout << p2 << "\n"; |
---|
57 | } |
---|