std::experimental::filesystem::is_symlink
来自 cppreference.cn
< cpp | experimental | fs
定义于头文件 <experimental/filesystem> |
||
bool is_symlink( file_status s ); |
(1) | (文件系统 TS) |
bool is_symlink( const path& p ); bool is_symlink( const path& p, error_code& ec ); |
(2) | (文件系统 TS) |
检查给定的文件状态或路径是否对应于符号链接,如同 POSIX S_IFLNK 所确定的一样。
1) 等价于 s.type() == file_type::symlink。
2) 等价于 is_symlink(status(p)) 或 is_symlink(status(p, ec))。
内容 |
[编辑] 参数
s | - | 要检查的文件状态 |
p | - | 要检查的路径 |
ec | - | 用于非抛出重载中的错误报告的输出参数 |
[编辑] 返回值
true 如果 p 指示的文件或者 s 指示的类型指的是符号链接。 如果发生错误,非抛出重载返回 false。
[编辑] 异常
1)
noexcept 规范:
noexcept
2) 不接受 error_code& 参数的重载会在底层 OS API 错误时抛出 filesystem_error,构造时使用 p 作为首个参数,以及 OS 错误代码作为错误代码参数。 如果内存分配失败,可能抛出 std::bad_alloc。 接受 error_code& 参数的重载会在 OS API 调用失败时将其设置为 OS API 错误代码,并且在没有错误发生时执行 ec.clear()。 此重载具有
noexcept 规范:
noexcept
[编辑] 注解
此函数提供的信息通常也作为目录迭代的副产品提供。 在目录迭代期间,调用 is_symlink(*iterator)
比 is_symlink(iterator->status())
效率低。
[编辑] 示例
运行此代码
#include <cstdio> #include <cstring> #include <experimental/filesystem> #include <fstream> #include <iostream> #include <sys/socket.h> #include <sys/stat.h> #include <sys/un.h> #include <unistd.h> namespace fs = std::experimental::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"); std::ofstream("sandbox/file"); // create regular 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"); // 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 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
[编辑] 参见
确定文件属性 确定文件属性,检查符号链接目标 (函数) | |
表示文件类型和权限 (类) | |
检查文件状态是否已知 (函数) | |
检查给定路径是否指向块设备 (函数) | |
检查给定路径是否指向字符设备 (函数) | |
检查给定路径是否指向目录 (函数) | |
检查给定路径是否指向命名管道 (函数) | |
检查参数是否指向其他文件 (函数) | |
检查参数是否指向常规文件 (函数) | |
检查参数是否指向命名 IPC 套接字 (函数) | |
检查路径是否指向现有的文件系统对象 (函数) | |
此目录条目指定文件的缓存状态 此目录条目指定文件的缓存符号链接状态 ( std::experimental::filesystem::directory_entry 的公共成员函数) |