std::filesystem::is_symlink
来自 cppreference.com
< cpp | filesystem
定义于头文件 <filesystem> |
||
bool is_symlink( std::filesystem::file_status s ) noexcept; |
(1) | (自 C++17) |
bool is_symlink( const std::filesystem::path& p ); |
(2) | (自 C++17) |
bool is_symlink( const std::filesystem::path& p, std::error_code& ec ) noexcept; |
(3) | (自 C++17) |
检查给定的文件状态或路径是否对应于符号链接,如同由 POSIX S_IFLNK 确定。
1) 等效于 s.type() == file_type::symlink.
2,3) 等效于 is_symlink(symlink_status(p)) 或 is_symlink(symlink_status(p, ec)).
内容 |
[编辑] 参数
s | - | 要检查的文件状态 |
p | - | 要检查的路径 |
ec | - | 非抛出重载中的错误报告输出参数 |
[编辑] 返回值
true 如果由 p 指示的文件或 s 指示的类型指的是符号链接。非抛出重载在发生错误时返回 false.
[编辑] 异常
任何未标记为 noexcept
的重载可能会在内存分配失败时抛出 std::bad_alloc.
[编辑] 示例
运行此代码
#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; // alternative: 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() { // 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
[编辑] 参见
(C++17)(C++17) |
确定文件属性 确定文件属性,检查符号链接目标 (函数) |
(C++17) |
表示文件类型和权限 (类) |
(C++17) |
检查文件状态是否已知 (函数) |
(C++17) |
检查给定的路径是否引用块设备 (函数) |
(C++17) |
检查给定的路径是否引用字符设备 (函数) |
(C++17) |
检查给定的路径是否引用目录 (函数) |
(C++17) |
检查给定的路径是否引用命名管道 (函数) |
(C++17) |
检查参数是否引用“其他”文件 (函数) |
(C++17) |
检查参数是否引用常规文件 (函数) |
(C++17) |
检查参数是否引用命名 IPC 套接字 (函数) |
(C++17) |
检查路径是否引用现有文件系统对象 (函数) |
检查目录项是否引用符号链接 ( std::filesystem::directory_entry 的公共成员函数) |