C++/Filesystem 文件类型
#include <iostream> #include <filesystem> #include <string> namespace fs = std::filesystem; void demo_status(const fs::path& p, fs::file_status s) { std::cout << p; switch (s.type()) { case fs::file_type::none: std::cout << " has `not-evaluated-yet` type"; break; case fs::file_type::not_found: std::cout << " does not exist"; break; case fs::file_type::regular: std::cout << " is a regular file"; break; case fs::file_type::directory: std::cout << " is a directory"; break; case fs::file_type::symlink: std::cout << " is a symlink"; break; case fs::file_type::block: std::cout << " is a block device"; break; case fs::file_type::character: std::cout << " is a character device"; break; case fs::file_type::fifo: std::cout << " is a named IPC pipe"; break; case fs::file_type::socket: std::cout << " is a named IPC socket"; break; case fs::file_type::unknown: std::cout << " has `unknown` type"; break; default: std::cout << " has `implementation-defined` type"; break; } std::cout << '\n'; } void demo_perms(fs::perms p) { using fs::perms; auto show = [=](char op, perms perm) { std::cout << (perms::none == (perm & p) ? '-' : op); }; show('r', perms::owner_read); show('w', perms::owner_write); show('x', perms::owner_exec); show('r', perms::group_read); show('w', perms::group_write); show('x', perms::group_exec); show('r', perms::others_read); show('w', perms::others_write); show('x', perms::others_exec); std::cout << '\n'; } int main() { const std::string path{ "../../../testdata/list.txt" }; demo_status(path, fs::status(path)); // 2. permissions demo_perms(fs::status(path).permissions()); // rwxrwxrwx return 0; }