std::filesystem::file_type

来自cppreference.com
 
 
 
在标头 <filesystem> 定义
enum class file_type {

    none = /* unspecified */,
    not_found = /* unspecified */,
    regular = /* unspecified */,
    directory = /* unspecified */,
    symlink = /* unspecified */,
    block = /* unspecified */,
    character = /* unspecified */,
    fifo = /* unspecified */,
    socket = /* unspecified */,
    unknown = /* unspecified */,
    /* implementation-defined */

};
(C++17 起)

file_type 定义常量,指示路径所指代的文件或目录的类型。枚举项的值各异。

常量

常量 含义
none 指示文件状态未被求值,或求值时出错
not_found 指示文件未找到(这不被认为是错误)
regular 常规文件
directory 目录
symlink 符号链接
block 块特殊文件
character 字符特殊文件
fifo FIFO (又称管道)文件
socket 套接字文件
实现定义 每个实现支持的附加文件类型的附加实现定义常量
unknown 文件存在但无法确定其类型


Example

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <filesystem>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.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::regular: ...}
    if(fs::is_regular_file(s)) std::cout << " is a regular file\n";
    if(fs::is_directory(s)) std::cout << " is a directory\n";
    if(fs::is_block_file(s)) std::cout << " is a block device\n";
    if(fs::is_character_file(s)) std::cout << " is a character device\n";
    if(fs::is_fifo(s)) std::cout << " is a named IPC pipe\n";
    if(fs::is_socket(s)) std::cout << " is a named IPC socket\n";
    if(fs::is_symlink(s)) std::cout << " is a symlink\n";
    if(!fs::exists(s)) std::cout << " does not exist\n";
}
int main()
{
    // 创建不同种类的文件
    fs::create_directory("sandbox");
    std::ofstream("sandbox/file"); // 创建常规文件
    fs::create_directory("sandbox/dir");
    mkfifo("sandbox/pipe", 0644);
    struct 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, (struct sockaddr*)&addr, sizeof addr);
    fs::create_symlink("file", "sandbox/symlink");
 
    // 演示不同的状态访问器
    for(auto it = fs::directory_iterator("sandbox"); it != fs::directory_iterator(); ++it)
        demo_status(*it, it->symlink_status()); // 从 directory_entry 使用缓冲的状态
    demo_status("dev/null", fs::status("/dev/null")); // 直接调用 status
    demo_status("dev/sda", fs::status("/dev/sda"));
    demo_status("sandbox/no", fs::status("/sandbox/no"));
 
    // 清理
    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

参阅

表示文件类型及权限
(类)
检查给定的路径是否表示块设备
(函数)
检查给定的路径是否表示字符设备
(函数)
检查给定的路径是否表示一个目录
(函数)
(C++17)
检查给定的路径是否表示一个命名管道
(函数)
(C++17)
检查参数是否表示一个其他文件
(函数)
(C++17)
检查参数是否表示一个具名 IPC 套接字
(函数)
检查参数是否表示一个符号链接
(函数)
检查 directory_entry 是否代表常规文件
(std::filesystem::directory_entry 的公开成员函数)