std::experimental::filesystem::directory_entry:: status, std::experimental::filesystem::directory_entry:: symlink_status
From cppreference.net
<
cpp
|
experimental
|
fs
|
directory entry
|
file_status status
(
)
const
;
file_status status ( error_code & ec ) const ; |
(1) | (文件系统 TS) |
|
file_status symlink_status
(
)
const
;
file_status symlink_status ( error_code & ec ) const ; |
(2) | (文件系统 TS) |
1)
返回条目的潜在缓存状态,如同通过
status
调用所确定(符号链接会被追踪至其目标)。
2)
返回条目的潜在缓存状态,如同通过
symlink_status
调用确定(不追踪符号链接)。
目录 |
参数
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
该条目所引用文件的状态。
异常
The overload that does not take an error_code & parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std:: bad_alloc may be thrown if memory allocation fails. The overload taking an error_code & parameter sets it to the OS API error code if an OS API call fails, and executes ec. clear ( ) if no errors occur. This overload has
noexcept
规范:
noexcept
备注
文件状态信息通常作为目录遍历的副产品提供,此时状态信息会被缓存,通过以下成员函数获取时无需额外系统调用开销。在目录遍历过程中,无需调用
status
函数,而应使用缓存的状态值调用
is_directory
等访问器,而非传入路径参数。
示例
运行此代码
#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; // 替代方案:switch(s.type()) { case fs::file_type::regular: ... } if (fs::is_regular_file(s)) std::cout << " 是常规文件\n"; if (fs::is_directory(s)) std::cout << " 是目录\n"; if (fs::is_block_file(s)) std::cout << " 是块设备\n"; if (fs::is_character_file(s)) std::cout << " 是字符设备\n"; if (fs::is_fifo(s)) std::cout << " 是命名IPC管道\n"; if (fs::is_socket(s)) std::cout << " 是命名IPC套接字\n"; if (fs::is_symlink(s)) std::cout << " 是符号链接\n"; if (!fs::exists(s)) std::cout << " 不存在\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()); // 使用目录条目中的缓存状态 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" 是常规文件 "sandbox/dir" 是目录 "sandbox/pipe" 是命名IPC管道 "sandbox/sock" 是命名IPC套接字 "sandbox/symlink" 是符号链接 "dev/null" 是字符设备 "dev/sda" 是块设备 "sandbox/no" 不存在
参见
|
表示文件类型和权限
(类) |
|
|
确定文件属性
确定文件属性,检查符号链接目标 (函数) |
|
|
检查文件状态是否已知
(函数) |
|
|
检查给定路径是否指向块设备
(函数) |
|
|
检查给定路径是否指向字符设备
(函数) |
|
|
检查给定路径是否指向目录
(函数) |
|
|
检查给定路径是否指向命名管道
(函数) |
|
|
检查参数是否指向
其他
文件
(函数) |
|
|
检查参数是否指向常规文件
(函数) |
|
|
检查参数是否指向命名IPC套接字
(函数) |
|
|
检查参数是否指向符号链接
(函数) |