代码
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
|
#include <ctime> #include <iostream> #include <filesystem>
int main() { namespace fs = std::filesystem;
auto testdir = fs::path("f:/testdir"); if (!fs::exists(testdir)) { std::cout << "file or directory is not exists!" << std::endl; }
fs::directory_options opt(fs::directory_options::none);
fs::directory_entry dir(testdir); std::cout << "show:\t" << dir.path().filename() << std::endl; for (fs::directory_entry const& entry : fs::directory_iterator(testdir, opt)) { if (entry.is_regular_file()) { std::cout << entry.path().filename() << "\t size: " << entry.file_size() << std::endl; } else if (entry.is_directory()) { std::cout << entry.path().filename() << "\t dir" << std::endl; } } std::cout << std::endl; std::cout << std::endl;
std::cout << "show all:\t" << dir.path().filename() << std::endl; for (fs::directory_entry const& entry : fs::recursive_directory_iterator(testdir, opt)) { if (entry.is_regular_file()) { std::cout << entry.path().filename() << "\t size: " << entry.file_size() << "\t parent: " << entry.path().parent_path() << std::endl; } else if (entry.is_directory()) { std::cout << entry.path().filename() << "\t dir" << std::endl; } } return 0; }
|
参考
https://zh.cppreference.com/w/cpp/filesystem
中文版
https://zh.cppreference.com/w/cpp/filesystem