std::filesystem::directory_entry:: is_symlink
From cppreference.net
<
cpp
|
filesystem
|
directory entry
|
bool
is_symlink
(
)
const
;
|
(1) | (自 C++17 起) |
|
bool
is_symlink
(
std::
error_code
&
ec
)
const
noexcept
;
|
(2) | (自 C++17 起) |
检查指向的对象是否为符号链接。实际效果相当于返回:
1)
std::
filesystem
::
is_symlink
(
symlink_status
(
)
)
,
2)
std::
filesystem
::
is_symlink
(
symlink_status
(
ec
)
)
。
目录 |
参数
| ec | - | 非抛出重载中用于错误报告的输出参数 |
返回值
true 表示所引用的文件系统对象为符号链接, false 则表示不是。
异常
任何未标记为
noexcept
的重载在内存分配失败时可能抛出
std::bad_alloc
。
示例
运行此代码
#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 << "does not exist "; if (entry.is_block_file()) std::cout << "is a block device "; if (entry.is_character_file()) std::cout << "is a character device "; if (entry.is_directory()) std::cout << "is a directory "; if (entry.is_fifo()) std::cout << "is a named IPC pipe "; if (entry.is_regular_file()) std::cout << "is a regular file "; if (entry.is_socket()) std::cout << "is a named IPC socket "; if (entry.is_symlink()) std::cout << "(a symlink)"; if (entry.is_other()) std::cout << "(an `other` file)"; 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 << "cleanup: 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 << "cleanup: close socket #" << *f << '\n'; close(*f); }}; bind(fd, reinterpret_cast<sockaddr*>(</
参见
|
(C++17)
|
检查参数是否指向符号链接
(函数) |