std::experimental::filesystem:: status, std::experimental::filesystem:: symlink_status
From cppreference.net
<
cpp
|
experimental
|
fs
|
定义于头文件
<experimental/filesystem>
|
||
|
file_status status
(
const
path
&
p
)
;
file_status status ( const path & p, error_code & ec ) noexcept ; |
(1) | (文件系统 TS) |
|
file_status symlink_status
(
const
path
&
p
)
;
file_status symlink_status ( const path & p, error_code & ec ) noexcept ; |
(2) | (文件系统 TS) |
1)
如同通过 POSIX
stat
确定由
p
标识的文件系统对象的类型和属性(符号链接会被追踪至其目标)。
-
- 若 p 是常规文件,返回 file_status ( file_type :: regular ) 。
- 若 p 是目录,返回 file_status ( file_type :: directory ) 。
- 若 p 是块特殊文件,返回 file_status ( file_type :: block ) 。
- 若 p 是字符特殊文件,返回 file_status ( file_type :: character ) 。
- 若 p 是 FIFO 或管道文件,返回 file_status ( file_type :: fifo ) 。
- 若 p 是套接字,返回 file_status ( file_type :: socket ) 。
- 若 p 不存在,返回 file_status ( file_type :: not_found ) 。
- 若 p 存在但无法确定文件属性(例如由于权限不足),返回 file_status ( file_type :: unknown ) 。
-
若错误导致甚至无法确认
p
是否存在:非抛出重载会设置
ec
并返回
file_status
(
file_type
::
none
)
,而抛出重载会抛出
filesystem_error。 - 其他情况返回 file_status ( file_type :: unknown ) 。
目录 |
参数
| p | - | 要检查的路径 |
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
文件状态(一个 file_status 对象)。
异常
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
注释
此函数提供的信息通常也会作为目录迭代的副产品提供,并可通过
directory_entry
的成员函数获取。在目录迭代期间,无需再次调用
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; // 替代方案: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套接字
(函数) |
|
|
检查参数是否指向符号链接
(函数) |
|
|
检查路径是否指向存在的文件系统对象
(函数) |
|
|
此目录条目所指文件的缓存状态
此目录条目所指文件的缓存符号链接状态 (
std::experimental::filesystem::directory_entry
的公开成员函数)
|