-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathcpp17-filesys1.cpp
76 lines (64 loc) · 2.56 KB
/
cpp17-filesys1.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <string>
#include <iterator>
#include <iomanip>
// C++17 - Requires compiler linking flag: -lstdc++fs on CLang or GCC.
#include <filesystem>
namespace fs = std::filesystem;
/** Iterate over first N entries of a file system iterator. */
template<typename Range, typename Function>
auto dotimes(size_t n, Range&& iterable, Function fun){
size_t i = 0;
auto it = fs::begin(iterable);
auto end = fs::end(iterable);
while(i < n && it != end ){
fun(it);
++it;
i++;
}
}
int main(){
std::cout << std::boolalpha;
std::cout << "\n EXPERIMENT 1 ===== Checking files in the system." << std::endl;
fs::path p1 = "/etc/iscsi/initiatorname.iscsi";
std::cout << " p1 = " << p1 << std::endl;
std::cout << "p1.string() = " << p1.string() << std::endl;
std::cout << "p1 ? exists = " << fs::exists(p1) << std::endl;
std::cout << "p1 ? is File = " << fs::is_regular_file(p1) << std::endl;
std::cout << "p1 ? is Dir = " << fs::is_directory(p1) << std::endl;
fs::path p2 = "/boot";
std::cout << " p2 = " << p2 << std::endl;
std::cout << "p2.string() = " << p2.string() << std::endl;
std::cout << "p2 ? exists = " << fs::exists(p2) << std::endl;
std::cout << "p2 ? is File = " << fs::is_regular_file(p2) << std::endl;
std::cout << "p2 ? is Dir = " << fs::is_directory(p2) << std::endl;
fs::path p3 = "/boot/does/not/exist";
std::cout << " p3 = " << p3 << std::endl;
std::cout << "p3.string() = " << p3.string() << std::endl;
std::cout << "p3 ? exists = " << fs::exists(p3) << std::endl;
std::cout << "p3 ? is File = " << fs::is_regular_file(p3) << std::endl;
std::cout << "p3 ? is Dir = " << fs::is_directory(p3) << std::endl;
std::cout << "\n EXPERIMENT 2 ===== Listing directory /etc =====" << std::endl;
// Show first 10 files of directory /etc
dotimes(10, fs::directory_iterator("/etc"),
[](auto p){
auto path = p->path();
std::cout << std::left
<< std::setw(0) << path.filename().string()
<< " " << std::setw(35)
<< std::right << std::setw(40) << path
<< std::endl;
});
std::cout << "\n EXPERIMENT 3 = Listing directory /etc (recursive) =====" << std::endl;
dotimes(20, fs::recursive_directory_iterator("/etc/"),
[](auto p){
std::cout << std::right
<< std::setw(10) << fs::is_directory(p->path())
<< std::setw(10) << fs::is_regular_file(p->path())
<< std::setw(10) << fs::is_symlink(p->path())
<< std::setw(10) << " "
<< std::setw(5) << std::left << p->path()
<< std::endl;
});
return EXIT_SUCCESS;
}