std::filesystem::file_type
来自 cppreference.com
< cpp | filesystem
定义在头文件 <filesystem> 中 |
||
enum class file_type { none = /* 未指定 */, |
(自 C++17 起) | |
file_type
定义常量,指示路径所指向的文件或目录的类型。枚举器的值是不同的。
[edit] 常量
常量 | 含义 |
none
|
表示文件状态尚未评估,或者在评估时发生了错误 |
not_found
|
表示未找到文件(这不算错误) |
regular
|
普通文件 |
directory
|
目录 |
symlink
|
符号链接 |
block
|
块特殊文件 |
character
|
字符特殊文件 |
fifo
|
FIFO(也称为管道)文件 |
socket
|
套接字文件 |
实现定义 | 为实现支持的每种附加文件类型定义一个附加的实现定义常量(例如,MSVC STL 为 NTFS 联接 定义 junction) |
unknown
|
文件存在,但无法确定其类型 |
[edit] 示例
运行此代码
#include <cstdio> #include <cstring> #include <filesystem> #include <fstream> #include <iostream> #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> #include <unistd.h> 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'; } int main() { // create files of different kinds fs::create_directory("sandbox"); fs::create_directory("sandbox/dir"); std::ofstream{"sandbox/file"}; // create regular file fs::create_symlink("file", "sandbox/symlink"); mkfifo("sandbox/pipe", 0644); sockaddr_un addr; addr.sun_family = AF_UNIX; std::strcpy(addr.sun_path, "sandbox/sock"); int fd = socket(PF_UNIX, SOCK_STREAM, 0); bind(fd, reinterpret_cast<sockaddr*>(&addr), sizeof addr); // demo different status accessors for (auto it{fs::directory_iterator("sandbox")}; it != fs::directory_iterator(); ++it) demo_status(*it, it->symlink_status()); // use cached status from directory entry demo_status("/dev/null", fs::status("/dev/null")); // direct calls to status demo_status("/dev/sda", fs::status("/dev/sda")); demo_status("sandbox/no", fs::status("/sandbox/no")); // cleanup (prefer std::unique_ptr-based custom deleters) close(fd); fs::remove_all("sandbox"); }
可能输出
"sandbox/file" is a regular file "sandbox/dir" is a directory "sandbox/pipe" is a named IPC pipe "sandbox/sock" is a named IPC socket "sandbox/symlink" is a symlink "/dev/null" is a character device "/dev/sda" is a block device "sandbox/no" does not exist
[edit] 另请参阅
(C++17) |
表示文件类型和权限 (类) |
(C++17) |
检查给定路径是否引用块设备 (函数) |
(C++17) |
检查给定路径是否引用字符设备 (函数) |
(C++17) |
检查给定路径是否引用目录 (函数) |
(C++17) |
检查给定路径是否引用命名管道 (函数) |
(C++17) |
检查参数是否引用其他文件 (函数) |
(C++17) |
检查参数是否引用命名 IPC 套接字 (函数) |
(C++17) |
检查参数是否引用符号链接 (函数) |
检查目录项是否引用普通文件 ( std::filesystem::directory_entry 的公共成员函数) |