forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8.1.filesystem.cpp
More file actions
51 lines (42 loc) 路 1.74 KB
/
Copy path8.1.filesystem.cpp
File metadata and controls
51 lines (42 loc) 路 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//
// 8.1.filesystem.cpp
// chapter 08 filesystem
// modern c++ tutorial
//
// created by changkun at changkun.de
// https://github.com/changkun/modern-cpp-tutorial
//
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
int main() {
// Work in a private scratch directory so the example is
// self-contained and repeatable.
const fs::path base = fs::temp_directory_path() / "modern-cpp-fs-demo";
fs::remove_all(base); // clean up any previous run
fs::create_directories(base / "sub"); // creates intermediate dirs
// Create a file.
std::ofstream(base / "hello.txt") << "hello, filesystem";
// Path decomposition (no filesystem access required).
const fs::path p = base / "hello.txt";
std::cout << "filename: " << p.filename() << "\n";
std::cout << "stem: " << p.stem() << "\n";
std::cout << "extension: " << p.extension() << "\n";
std::cout << "parent: " << p.parent_path() << "\n";
// Query the file.
std::cout << "exists: " << fs::exists(p) << "\n";
std::cout << "is_regular_file: " << fs::is_regular_file(p) << "\n";
std::cout << "file_size: " << fs::file_size(p) << "\n";
// Recursively iterate the directory tree.
std::cout << "entries:\n";
for (const auto& entry : fs::recursive_directory_iterator(base))
std::cout << " " << entry.path() << "\n";
// Copy, then rename.
fs::copy_file(p, base / "copy.txt");
fs::rename(base / "copy.txt", base / "renamed.txt");
std::cout << "renamed exists: " << fs::exists(base / "renamed.txt") << "\n";
// Clean up.
fs::remove_all(base);
std::cout << "after cleanup, exists: " << fs::exists(base) << "\n";
}