列出目录下的所有文件
How to do it...
#include <iostream> #include <sstream> #include <iomanip> #include <numeric> #include <algorithm> #include <vector> #include <filesystem> using namespace std; using namespace filesystem;static tuple<path, file_status, size_t> file_info(const directory_entry &entry) { const auto fs (status(entry)); return {entry.path(), fs, is_regular_file(fs) ? file_size(entry.path()) : 0u}; }static char type_char(file_status fs) { if (is_directory(fs)) { return 'd'; } else if (is_symlink(fs)) { return 'l'; } else if (is_character_file(fs)) { return 'c'; } else if (is_block_file(fs)) { return 'b'; } else if (is_fifo(fs)) { return 'p'; } else if (is_socket(fs)) { return 's'; } else if (is_other(fs)) { return 'o'; } else if (is_regular_file(fs)) { return 'f'; } return '?'; }static string rwx(perms p) { auto check ([p](perms bit, char c) { return (p & bit) == perms::none ? '-' : c; }); return {check(perms::owner_read, 'r'), check(perms::owner_write, 'w'), check(perms::owner_exec, 'x'), check(perms::group_read, 'r'), check(perms::group_write, 'w'), check(perms::group_exec, 'x'), check(perms::others_read, 'r'), check(perms::others_write, 'w'), check(perms::others_exec, 'x')}; }static string size_string(size_t size) { stringstream ss; if (size >= 1000000000) { ss << (size / 1000000000) << 'G'; } else if (size >= 1000000) { ss << (size / 1000000) << 'M'; } else if (size >= 1000) { ss << (size / 1000) << 'K'; } else { ss << size << 'B'; } return ss.str(); }int main(int argc, char *argv[]) { path dir {argc > 1 ? argv[1] : "."}; if (!exists(dir)) { cout << "Path " << dir << " does not exist.\n"; return 1; }vector<tuple<path, file_status, size_t>> items; transform(directory_iterator{dir}, {}, back_inserter(items), file_info);for (const auto &[path, status, size] : items) { cout << type_char(status) << rwx(status.permissions()) << " " << setw(4) << right << size_string(size) << " " << path.filename().c_str() << '\n'; } }$ ./list ~/Documents/cpp_reference/en/cpp drwxrwxr-x 0B algorithm frw-r--r-- 88K algorithm.html drwxrwxr-x 0B atomic frw-r--r-- 35K atomic.html drwxrwxr-x 0B chrono frw-r--r-- 34K chrono.html frw-r--r-- 21K comment.html frw-r--r-- 21K comments.html frw-r--r-- 220K compiler_support.html drwxrwxr-x 0B concept frw-r--r-- 67K concept.html drwxr-xr-x 0B container frw-r--r-- 285K container.html drwxrwxr-x 0B error frw-r--r-- 52K error.html
How it works...
Last updated