标准库头文件 <filesystem> (C++17)
来自 cppreference.com
此头文件是 文件系统支持 库的一部分。
包含 | ||
(C++20) |
三元比较运算符 支持 | |
类 | ||
在命名空间
std::filesystem 中定义 | ||
(C++17) |
表示路径 (类) | |
(C++17) |
在文件系统错误时抛出的异常 (类) | |
(C++17) |
目录条目 (类) | |
(C++17) |
目录内容的迭代器 (类) | |
(C++17) |
目录及其子目录内容的迭代器 (类) | |
(C++17) |
表示文件类型和权限 (类) | |
(C++17) |
有关文件系统上可用空间和空闲空间的信息 (类) | |
(C++17) |
文件的类型 (枚举) | |
(C++17) |
识别文件系统权限 (枚举) | |
(C++17) |
指定权限操作的语义 (枚举) | |
(C++17) |
指定复制操作的语义 (枚举) | |
(C++17) |
迭代目录内容的选项 (枚举) | |
(C++17) |
表示文件时间值 (typedef) | |
在命名空间
std 中定义 | ||
对 std::filesystem::path 的哈希支持 (类模板特化) | ||
前向声明 | ||
在头文件
<functional> 中定义 | ||
在命名空间
std 中定义 | ||
(C++11) |
哈希函数对象 (类模板) | |
函数 | ||
在命名空间
std::filesystem 中定义 | ||
(C++17)(C++20 中已弃用) |
从 UTF-8 编码的源创建 path (函数) | |
(C++17) |
组合绝对路径 (函数) | |
(C++17) |
组合规范路径 (函数) | |
(C++17) |
组合相对路径 (函数) | |
(C++17) |
复制文件或目录 (函数) | |
(C++17) |
复制文件内容 (函数) | |
(C++17) |
复制符号链接 (函数) | |
(C++17)(C++17) |
创建新目录 (函数) | |
(C++17) |
创建硬链接 (函数) | |
(C++17)(C++17) |
创建符号链接 (函数) | |
(C++17) |
返回或设置当前工作目录 (函数) | |
(C++17) |
检查路径是否引用现有的文件系统对象 (函数) | |
(C++17) |
检查两个路径是否引用同一个文件系统对象 (函数) | |
(C++17) |
返回文件的大小 (函数) | |
(C++17) |
返回引用特定文件的硬链接数量 (函数) | |
(C++17) |
获取或设置上次数据修改的时间 (函数) | |
(C++17) |
修改文件访问权限 (函数) | |
(C++17) |
获取符号链接的目标 (函数) | |
(C++17)(C++17) |
删除文件或空目录 递归删除文件或目录及其所有内容 (函数) | |
(C++17) |
移动或重命名文件或目录 (函数) | |
(C++17) |
通过截断或零填充更改常规文件的大小 (函数) | |
(C++17) |
确定文件系统上的可用空闲空间 (函数) | |
(C++17)(C++17) |
确定文件属性 确定文件属性,检查符号链接目标 (函数) | |
(C++17) |
返回适合临时文件的目录 (函数) | |
文件类型 | ||
(C++17) |
检查给定路径是否引用块设备 (函数) | |
(C++17) |
检查给定路径是否引用字符设备 (函数) | |
(C++17) |
检查给定路径是否引用目录 (函数) | |
(C++17) |
检查给定路径是否引用空文件或目录 (函数) | |
(C++17) |
检查给定路径是否引用命名管道 (函数) | |
(C++17) |
检查参数是否引用其他文件 (函数) | |
(C++17) |
检查参数是否引用常规文件 (函数) | |
(C++17) |
检查参数是否引用命名 IPC 套接字 (函数) | |
(C++17) |
检查参数是否引用符号链接 (函数) | |
(C++17) |
检查文件状态是否已知 (函数) |
[编辑] 概述
#include <compare> namespace std::filesystem { // paths class path; // path non-member functions void swap(path& lhs, path& rhs) noexcept; size_t hash_value(const path& p) noexcept; // filesystem errors class filesystem_error; // directory entries class directory_entry; // directory iterators class directory_iterator; // range access for directory iterators directory_iterator begin(directory_iterator iter) noexcept; directory_iterator end(directory_iterator) noexcept; // recursive directory iterators class recursive_directory_iterator; // range access for recursive directory iterators recursive_directory_iterator begin(recursive_directory_iterator iter) noexcept; recursive_directory_iterator end(recursive_directory_iterator) noexcept; // file status class file_status; struct space_info { uintmax_t capacity; uintmax_t free; uintmax_t available; friend bool operator==(const space_info&, const space_info&) = default; }; // enumerations enum class file_type; enum class perms; enum class perm_options; enum class copy_options; enum class directory_options; using file_time_type = chrono::time_point<chrono::file_clock>; // filesystem operations path absolute(const path& p); path absolute(const path& p, error_code& ec); path canonical(const path& p); path canonical(const path& p, error_code& ec); void copy(const path& from, const path& to); void copy(const path& from, const path& to, error_code& ec); void copy(const path& from, const path& to, copy_options options); void copy(const path& from, const path& to, copy_options options, error_code& ec); bool copy_file(const path& from, const path& to); bool copy_file(const path& from, const path& to, error_code& ec); bool copy_file(const path& from, const path& to, copy_options option); bool copy_file(const path& from, const path& to, copy_options option, error_code& ec); void copy_symlink(const path& existing_symlink, const path& new_symlink); void copy_symlink(const path& existing_symlink, const path& new_symlink, error_code& ec) noexcept; bool create_directories(const path& p); bool create_directories(const path& p, error_code& ec); bool create_directory(const path& p); bool create_directory(const path& p, error_code& ec) noexcept; bool create_directory(const path& p, const path& attributes); bool create_directory(const path& p, const path& attributes, error_code& ec) noexcept; void create_directory_symlink(const path& to, const path& new_symlink); void create_directory_symlink(const path& to, const path& new_symlink, error_code& ec) noexcept; void create_hard_link(const path& to, const path& new_hard_link); void create_hard_link(const path& to, const path& new_hard_link, error_code& ec) noexcept; void create_symlink(const path& to, const path& new_symlink); void create_symlink(const path& to, const path& new_symlink, error_code& ec) noexcept; path current_path(); path current_path(error_code& ec); void current_path(const path& p); void current_path(const path& p, error_code& ec) noexcept; bool equivalent(const path& p1, const path& p2); bool equivalent(const path& p1, const path& p2, error_code& ec) noexcept; bool exists(file_status s) noexcept; bool exists(const path& p); bool exists(const path& p, error_code& ec) noexcept; uintmax_t file_size(const path& p); uintmax_t file_size(const path& p, error_code& ec) noexcept; uintmax_t hard_link_count(const path& p); uintmax_t hard_link_count(const path& p, error_code& ec) noexcept; bool is_block_file(file_status s) noexcept; bool is_block_file(const path& p); bool is_block_file(const path& p, error_code& ec) noexcept; bool is_character_file(file_status s) noexcept; bool is_character_file(const path& p); bool is_character_file(const path& p, error_code& ec) noexcept; bool is_directory(file_status s) noexcept; bool is_directory(const path& p); bool is_directory(const path& p, error_code& ec) noexcept; bool is_empty(const path& p); bool is_empty(const path& p, error_code& ec); bool is_fifo(file_status s) noexcept; bool is_fifo(const path& p); bool is_fifo(const path& p, error_code& ec) noexcept; bool is_other(file_status s) noexcept; bool is_other(const path& p); bool is_other(const path& p, error_code& ec) noexcept; bool is_regular_file(file_status s) noexcept; bool is_regular_file(const path& p); bool is_regular_file(const path& p, error_code& ec) noexcept; bool is_socket(file_status s) noexcept; bool is_socket(const path& p); bool is_socket(const path& p, error_code& ec) noexcept; bool is_symlink(file_status s) noexcept; bool is_symlink(const path& p); bool is_symlink(const path& p, error_code& ec) noexcept; file_time_type last_write_time(const path& p); file_time_type last_write_time(const path& p, error_code& ec) noexcept; void last_write_time(const path& p, file_time_type new_time); void last_write_time(const path& p, file_time_type new_time, error_code& ec) noexcept; void permissions(const path& p, perms prms, perm_options opts=perm_options::replace); void permissions(const path& p, perms prms, error_code& ec) noexcept; void permissions(const path& p, perms prms, perm_options opts, error_code& ec); path proximate(const path& p, error_code& ec); path proximate(const path& p, const path& base = current_path()); path proximate(const path& p, const path& base, error_code& ec); path read_symlink(const path& p); path read_symlink(const path& p, error_code& ec); path relative(const path& p, error_code& ec); path relative(const path& p, const path& base = current_path()); path relative(const path& p, const path& base, error_code& ec); bool remove(const path& p); bool remove(const path& p, error_code& ec) noexcept; uintmax_t remove_all(const path& p); uintmax_t remove_all(const path& p, error_code& ec); void rename(const path& from, const path& to); void rename(const path& from, const path& to, error_code& ec) noexcept; void resize_file(const path& p, uintmax_t size); void resize_file(const path& p, uintmax_t size, error_code& ec) noexcept; space_info space(const path& p); space_info space(const path& p, error_code& ec) noexcept; file_status status(const path& p); file_status status(const path& p, error_code& ec) noexcept; bool status_known(file_status s) noexcept; file_status symlink_status(const path& p); file_status symlink_status(const path& p, error_code& ec) noexcept; path temp_directory_path(); path temp_directory_path(error_code& ec); path weakly_canonical(const path& p); path weakly_canonical(const path& p, error_code& ec); } // hash support namespace std { template<class T> struct hash; template<> struct hash<filesystem::path>; } namespace std::ranges { template<> inline constexpr bool enable_borrowed_range<filesystem::directory_iterator> = true; template<> inline constexpr bool enable_borrowed_range<filesystem::recursive_directory_iterator> = true; template<> inline constexpr bool enable_view<filesystem::directory_iterator> = true; template<> inline constexpr bool enable_view<filesystem::recursive_directory_iterator> = true; }
[编辑] 类 std::filesystem::path
namespace std::filesystem { class path { public: using value_type = /* see description */; using string_type = basic_string<value_type>; static constexpr value_type preferred_separator = /* see description */; // enumeration format enum format; // constructors and destructor path() noexcept; path(const path& p); path(path&& p) noexcept; path(string_type&& source, format fmt = auto_format); template<class Source> path(const Source& source, format fmt = auto_format); template<class InputIt> path(InputIt first, InputIt last, format fmt = auto_format); template<class Source> path(const Source& source, const locale& loc, format fmt = auto_format); template<class InputIt> path(InputIt first, InputIt last, const locale& loc, format fmt = auto_format); ~path(); // assignments path& operator=(const path& p); path& operator=(path&& p) noexcept; path& operator=(string_type&& source); path& assign(string_type&& source); template<class Source> path& operator=(const Source& source); template<class Source> path& assign(const Source& source); template<class InputIt> path& assign(InputIt first, InputIt last); // appends path& operator/=(const path& p); template<class Source> path& operator/=(const Source& source); template<class Source> path& append(const Source& source); template<class InputIt> path& append(InputIt first, InputIt last); // concatenation path& operator+=(const path& x); path& operator+=(const string_type& x); path& operator+=(basic_string_view<value_type> x); path& operator+=(const value_type* x); path& operator+=(value_type x); template<class Source> path& operator+=(const Source& x); template<class ECharT> path& operator+=(ECharT x); template<class Source> path& concat(const Source& x); template<class InputIt> path& concat(InputIt first, InputIt last); // modifiers void clear() noexcept; path& make_preferred(); path& remove_filename(); path& replace_filename(const path& replacement); path& replace_extension(const path& replacement = path()); void swap(path& rhs) noexcept; // non-member operators friend bool operator==(const path& lhs, const path& rhs) noexcept; friend strong_ordering operator<=>(const path& lhs, const path& rhs) noexcept; friend path operator/ (const path& lhs, const path& rhs); // native format observers const string_type& native() const noexcept; const value_type* c_str() const noexcept; operator string_type() const; template<class ECharT, class Traits = char_traits<ECharT>, class Allocator = allocator<ECharT>> basic_string<ECharT, Traits, Allocator> string(const Allocator& a = Allocator()) const; std::string string() const; std::wstring wstring() const; std::u8string u8string() const; std::u16string u16string() const; std::u32string u32string() const; // generic format observers template<class ECharT, class Traits = char_traits<ECharT>, class Allocator = allocator<ECharT>> basic_string<ECharT, Traits, Allocator> generic_string(const Allocator& a = Allocator()) const; std::string generic_string() const; std::wstring generic_wstring() const; std::u8string generic_u8string() const; std::u16string generic_u16string() const; std::u32string generic_u32string() const; // compare int compare(const path& p) const noexcept; int compare(const string_type& s) const; int compare(basic_string_view<value_type> s) const; int compare(const value_type* s) const; // decomposition path root_name() const; path root_directory() const; path root_path() const; path relative_path() const; path parent_path() const; path filename() const; path stem() const; path extension() const; // query bool empty() const noexcept; bool has_root_name() const; bool has_root_directory() const; bool has_root_path() const; bool has_relative_path() const; bool has_parent_path() const; bool has_filename() const; bool has_stem() const; bool has_extension() const; bool is_absolute() const; bool is_relative() const; // generation path lexically_normal() const; path lexically_relative(const path& base) const; path lexically_proximate(const path& base) const; // iterators class iterator; using const_iterator = iterator; iterator begin() const; iterator end() const; // path inserter and extractor template<class CharT, class Traits> friend basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& os, const path& p); template<class CharT, class Traits> friend basic_istream<CharT, Traits>& operator>>(basic_istream<CharT, Traits>& is, path& p); }; }
[编辑] 类 std::filesystem::filesystem_error
namespace std::filesystem { class filesystem_error : public system_error { public: filesystem_error(const string& what_arg, error_code ec); filesystem_error(const string& what_arg, const path& p1, error_code ec); filesystem_error(const string& what_arg, const path& p1, const path& p2, error_code ec); const path& path1() const noexcept; const path& path2() const noexcept; const char* what() const noexcept override; }; }
[编辑] 类 std::filesystem::directory_entry
namespace std::filesystem { class directory_entry { public: // constructors and destructor directory_entry() noexcept = default; directory_entry(const directory_entry&) = default; directory_entry(directory_entry&&) noexcept = default; explicit directory_entry(const filesystem::path& p); directory_entry(const filesystem::path& p, error_code& ec); ~directory_entry(); // assignments directory_entry& operator=(const directory_entry&) = default; directory_entry& operator=(directory_entry&&) noexcept = default; // modifiers void assign(const filesystem::path& p); void assign(const filesystem::path& p, error_code& ec); void replace_filename(const filesystem::path& p); void replace_filename(const filesystem::path& p, error_code& ec); void refresh(); void refresh(error_code& ec) noexcept; // observers const filesystem::path& path() const noexcept; operator const filesystem::path&() const noexcept; bool exists() const; bool exists(error_code& ec) const noexcept; bool is_block_file() const; bool is_block_file(error_code& ec) const noexcept; bool is_character_file() const; bool is_character_file(error_code& ec) const noexcept; bool is_directory() const; bool is_directory(error_code& ec) const noexcept; bool is_fifo() const; bool is_fifo(error_code& ec) const noexcept; bool is_other() const; bool is_other(error_code& ec) const noexcept; bool is_regular_file() const; bool is_regular_file(error_code& ec) const noexcept; bool is_socket() const; bool is_socket(error_code& ec) const noexcept; bool is_symlink() const; bool is_symlink(error_code& ec) const noexcept; uintmax_t file_size() const; uintmax_t file_size(error_code& ec) const noexcept; uintmax_t hard_link_count() const; uintmax_t hard_link_count(error_code& ec) const noexcept; file_time_type last_write_time() const; file_time_type last_write_time(error_code& ec) const noexcept; file_status status() const; file_status status(error_code& ec) const noexcept; file_status symlink_status() const; file_status symlink_status(error_code& ec) const noexcept; bool operator==(const directory_entry& rhs) const noexcept; strong_ordering operator<=>(const directory_entry& rhs) const noexcept; // inserter template<class CharT, class Traits> friend basic_ostream<CharT, Traits>& operator<<(basic_ostream<CharT, Traits>& os, const directory_entry& d); private: filesystem::path pathobject; // exposition only friend class directory_iterator; // exposition only }; }
[编辑] 类 std::filesystem::directory_iterator
namespace std::filesystem { class directory_iterator { public: using iterator_category = input_iterator_tag; using value_type = directory_entry; using difference_type = ptrdiff_t; using pointer = const directory_entry*; using reference = const directory_entry&; // member functions directory_iterator() noexcept; explicit directory_iterator(const path& p); directory_iterator(const path& p, directory_options options); directory_iterator(const path& p, error_code& ec); directory_iterator(const path& p, directory_options options, error_code& ec); directory_iterator(const directory_iterator& rhs); directory_iterator(directory_iterator&& rhs) noexcept; ~directory_iterator(); directory_iterator& operator=(const directory_iterator& rhs); directory_iterator& operator=(directory_iterator&& rhs) noexcept; const directory_entry& operator*() const; const directory_entry* operator->() const; directory_iterator& operator++(); directory_iterator& increment(error_code& ec); bool operator==(default_sentinel_t) const noexcept { return *this == directory_iterator(); } // other members as required by input iterators }; }
[编辑] 类 std::filesystem::recursive_directory_iterator
namespace std::filesystem { class recursive_directory_iterator { public: using iterator_category = input_iterator_tag; using value_type = directory_entry; using difference_type = ptrdiff_t; using pointer = const directory_entry*; using reference = const directory_entry&; // constructors and destructor recursive_directory_iterator() noexcept; explicit recursive_directory_iterator(const path& p); recursive_directory_iterator(const path& p, directory_options options); recursive_directory_iterator(const path& p, directory_options options, error_code& ec); recursive_directory_iterator(const path& p, error_code& ec); recursive_directory_iterator(const recursive_directory_iterator& rhs); recursive_directory_iterator(recursive_directory_iterator&& rhs) noexcept; ~recursive_directory_iterator(); // observers directory_options options() const; int depth() const; bool recursion_pending() const; const directory_entry& operator*() const; const directory_entry* operator->() const; // modifiers recursive_directory_iterator& operator=(const recursive_directory_iterator& rhs); recursive_directory_iterator& operator=(recursive_directory_iterator&& rhs) noexcept; recursive_directory_iterator& operator++(); recursive_directory_iterator& increment(error_code& ec); void pop(); void pop(error_code& ec); void disable_recursion_pending(); bool operator==(default_sentinel_t) const noexcept { return *this == recursive_directory_iterator(); } // other members as required by input iterators }; }
[编辑] 类 std::filesystem::file_status
namespace std::filesystem { class file_status { public: // constructors and destructor file_status() noexcept : file_status(file_type::none) {} explicit file_status(file_type ft, perms prms = perms::unknown) noexcept; file_status(const file_status&) noexcept = default; file_status(file_status&&) noexcept = default; ~file_status(); // assignments file_status& operator=(const file_status&) noexcept = default; file_status& operator=(file_status&&) noexcept = default; // modifiers void type(file_type ft) noexcept; void permissions(perms prms) noexcept; // observers file_type type() const noexcept; perms permissions() const noexcept; friend bool operator==(const file_status& lhs, const file_status& rhs) noexcept { return lhs.type() == rhs.type() && lhs.permissions() == rhs.permissions(); } }; }