-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
27 lines (22 loc) · 834 Bytes
/
test.cpp
File metadata and controls
27 lines (22 loc) · 834 Bytes
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
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
// Define the path to list the contents from
fs::path p = fs::current_path(); // or specify any other path
// Try to list the directory contents
try {
// Check if the path is a directory
if (fs::is_directory(p)) {
std::cout << "Listing contents of directory: " << p << std::endl;
for (const auto& entry : fs::directory_iterator(p)) {
std::cout << entry.path().filename() << std::endl;
}
} else {
std::cerr << p << " is not a valid directory." << std::endl;
}
} catch (const fs::filesystem_error& e) {
std::cerr << "Error accessing the filesystem: " << e.what() << std::endl;
}
return 0;
}