std::filesystem::directory_entry::assign
来自 cppreference.com
< cpp | filesystem | directory entry
void assign( const std::filesystem::path& p ); |
(1) | (自 C++17) |
void assign( const std::filesystem::path& p, std::error_code& ec ); |
(2) | (自 C++17) |
将新内容分配给目录条目对象。将路径设置为 p 并调用 refresh
以更新缓存的属性。如果发生错误,缓存的属性的值将不确定。
此函数不会对文件系统进行任何更改。
内容 |
[编辑] 参数
p | - | 指向目录条目将引用的文件系统对象的路径 |
ec | - | 非抛出重载中用于错误报告的输出参数 |
[编辑] 返回值
(无)
[编辑] 异常
任何未标记为 noexcept
的重载可能会在内存分配失败时抛出 std::bad_alloc。
[编辑] 示例
运行此代码
#include <filesystem> #include <fstream> #include <iostream> void print_entry_info(const std::filesystem::directory_entry& entry) { if (std::cout << "The entry " << entry; not entry.exists()) { std::cout << " does not exists on the file system\n"; return; } std::cout << " is "; if (entry.is_directory()) std::cout << "a directory\n"; if (entry.is_regular_file()) std::cout << "a regular file\n"; /*...*/ } int main() { std::filesystem::current_path(std::filesystem::temp_directory_path()); std::filesystem::directory_entry entry{std::filesystem::current_path()}; print_entry_info(entry); std::filesystem::path name{"cppreference.html"}; std::ofstream{name} << "C++"; std::cout << "entry.assign();\n"; entry.assign(entry/name); print_entry_info(entry); std::cout << "remove(entry);\n"; std::filesystem::remove(entry); print_entry_info(entry); // the entry still contains old "state" std::cout << "entry.assign();\n"; entry.assign(entry); // or just call entry.refresh() print_entry_info(entry); }
可能的输出
The entry "/tmp" is a directory entry.assign(); The entry "/tmp/cppreference.html" is a regular file remove(entry); The entry "/tmp/cppreference.html" is a regular file entry.assign(); The entry "/tmp/cppreference.html" does not exists on the file system
[编辑] 参见
分配内容 (公共成员函数) |