Namespaces
Variants

std::filesystem::directory_entry:: is_fifo

From cppreference.net
bool is_fifo ( ) const ;
(1) (自 C++17 起)
bool is_fifo ( std:: error_code & ec ) const noexcept ;
(2) (自 C++17 起)

检查所指向的对象是否为FIFO或管道文件。实际返回:

1) std:: filesystem :: is_fifo ( status ( ) )
2) std:: filesystem :: is_fifo ( status ( ec ) ) .

目录

参数

ec - 非抛出重载中用于错误报告的输出参数

返回值

true 如果引用的文件系统对象是 FIFO 或管道文件, false 表示其他情况。

异常

任何未标记为 noexcept 的重载在内存分配失败时可能抛出 std::bad_alloc

1) 当底层操作系统API出错时抛出 std::filesystem::filesystem_error ,该异常以 p 作为第一个路径参数,以操作系统错误代码作为错误码参数构造。
2) 若操作系统API调用失败,将 std:: error_code & 参数设置为操作系统API错误码;若无错误发生,则执行 ec. clear ( )

示例

#include <cstdio>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
namespace fs = std::filesystem;
void print_entry_type(const std::filesystem::directory_entry& entry)
{
    std::cout << entry.path() << ": ";
    if (!entry.exists())
        std::cout << "不存在 ";
    if (entry.is_block_file())
        std::cout << "是块设备 ";
    if (entry.is_character_file())
        std::cout << "是字符设备 ";
    if (entry.is_directory())
        std::cout << "是目录 ";
    if (entry.is_fifo())
        std::cout << "是命名IPC管道 ";
    if (entry.is_regular_file())
        std::cout << "是常规文件 ";
    if (entry.is_socket())
        std::cout << "是命名IPC套接字 ";
    if (entry.is_symlink())
        std::cout << "(符号链接)";
    if (entry.is_other())
        std::cout << "(`其他`文件)";
    std::cout << '\n';
}
template<typename Type, typename Fun>
class scoped_cleanup
{
    std::unique_ptr<Type, std::function<void(const Type*)>> u;
public:
    scoped_cleanup(Type* ptr, Fun fun) : u{ptr, std::move(fun)} {}
};
int main()
{
    // 创建不同类型的文件
    std::filesystem::current_path(fs::temp_directory_path());
    const std::filesystem::path sandbox{"sandbox"};
    scoped_cleanup remove_all_at_exit{&sandbox, [](const fs::path* p)
    {
        std::cout << "清理: remove_all(" << *p << ")\n";
        fs::remove_all(*p);
    }};
    std::filesystem::create_directory(sandbox);
    std::ofstream{sandbox/"file"}; // 创建常规文件
    std::filesystem::create_directory(sandbox/"dir");
    mkfifo((sandbox/"pipe").string().data(), 0644);
    struct sockaddr_un addr; addr.sun_family = AF_UNIX;
    std::strcpy(addr.sun_path, (sandbox/"sock").string().data());
    int fd{socket(PF_UNIX, SOCK_STREAM, 0)};
    scoped_cleanup close_socket_at_exit{&fd, [](const int* f)
    {
        std::cout << "清理: 关闭套接字 #" << *f << '\n';
        close(*f);
    }};
    bind(fd, reinterpret_cast<sockaddr*>(<a href="../../memory

参见

(C++17)
检查给定路径是否指向命名管道
(函数)