filesystem
filesystem 库是 C++17 中引入的一个重要特性,它提供了一系列用于文件系统操作的功能。这个库包含在 <filesystem> 头文件中,并且定义在 std::filesystem 命名空间内。
常用的类(前三个较为常用)
1. std::filesystem::path
- 描述:表示文件系统中的路径。
 
构造函数:path(const char* p) 或 path(const std::string& p):构造路径。
- **
operator/**:path operator/(const path& rhs) const:支持路径拼接。 - **
filename()**:path filename() const:返回路径的最后一部分(包括后缀的文件名)。 - **
extension()**:path extension() const:返回文件的扩展名(例如.txt)。 - **
stem()**:path stem() const:返回文件除去后缀的名字(例如.txt)。 - **
parent_path()**:path parent_path() const:返回去掉文件名后的路径部分。 - **
string()**:std::string string() const:将路径转换为std::string类型。 - **
wstring**():std::wstring string() const:将路径转换为std::wstring类型。 
2. std::filesystem::directory_entry
描述:表示目录中的单个条目(文件或目录)。
常用函数:
- **
path()**:path path() const:返回该条目的完整路径。 - **
is_directory()**:bool is_directory() const:检查该条目是否是目录。 - **
is_regular_file()**:bool is_regular_file() const:检查该条目是否是常规文件。 - **
file_size()**:std::uintmax_t file_size() const:获取文件的大小。 - **
last_write_time()**:std::filesystem::file_time_type last_write_time() const:获取最后修改时间。 
- **
 
3. std::filesystem::directory_iterator
描述:用于遍历指定目录中的条目。
常用函数:
- **
operator++()**:directory_iterator& operator++():移动到下一个条目。 - **
operator\*()**:directory_entry operator*() const:解引用,获取当前条目。 - **
operator!=()**:bool operator!=(const directory_iterator& other) const:比较两个迭代器,判断它们是否不相等。 
- **
 
4. std::filesystem::recursive_directory_iterator
描述:用于递归遍历指定目录及其子目录中的条目。
常用函数:
- **
operator++()**:recursive_directory_iterator& operator++():移动到下一个条目(包括子目录)。 - **
operator\*()**:directory_entry operator*() const:解引用,获取当前条目。 - **
operator!=()**:bool operator!=(const recursive_directory_iterator& other) const:比较两个迭代器,判断它们是否不相等。 
- **
 
5. std::filesystem::file_status
描述:表示文件或目录的状态。
常用函数:
- **
type()**:file_type type() const:获取文件的类型。 - **
permissions()**:perms permissions() const:获取权限信息。 
- **
 
6. std::filesystem::space_info
描述:表示磁盘空间的信息。
成员变量:
- **
capacity**:std::uintmax_t capacity:磁盘的总容量。 - **
free**:std::uintmax_t free:可用空间。 - **
available**:std::uintmax_t available:当前用户可用的空间。 
- **
 
7. std::filesystem::file_type
描述:枚举类型,表示文件的类型。
常见值:
file_type::regular:常规文件。file_type::directory:目录。file_type::symlink:符号链接。file_type::unknown:未知类型。
8. std::filesystem::permissions
描述:表示文件或目录的权限类型,可以组合使用。
常见权限值:
perms::none:无权限。perms::owner_read:所有者读取权限。perms::owner_write:所有者写入权限。perms::owner_exe:所有者执行权限。
常用库函数
void copy(const path& from, const path& to) :目录复制
path absolute(const path& pval, const path& base = current_path()) :获取相对于base的绝对路径
bool create_directory(const path& pval) :当目录不存在时创建目录
bool create_directories(const path& pval) :形如/a/b/c这样的,如果都不存在,创建目录结构
bool exists(const path& pval) :用于判断path是否存在
uintmax_t file_size(const path& pval) :返回目录的大小
file_time_type last_write_time(const path& pval) :返回目录最后修改日期的file_time_type对象
bool remove(const path& pval) :删除目录
uintmax_t remove_all(const path& pval) :递归删除目录下所有文件,返回被成功删除的文件个数
void rename(const path& from, const path& to) :移动文件或者重命名
示例
    fs::path resourceDir = "resources";
    // 遍历目录
    for (const auto& entry : fs::directory_iterator(resourceDir)) {
        if (entry.is_regular_file()) 
        { // 确保是文件而不是子目录
            IMAGE* image = new IMAGE();
            loadimage(image, entry.path().wstring().c_str());
            if (!check_image_valid(image))
                throw entry.path().wstring().c_str();
            image_pool[entry.path().stem().string()] = image;
        }
        else
        {
            fs::path filePath = entry.path();
            if (filePath.filename() == "player")
            {
                
                for (const auto& m_entry : fs::directory_iterator(filePath))
                {
                    IMAGE* image = new IMAGE();
                    loadimage(image,m_entry.path().wstring().c_str());
                    if (!check_image_valid(image))
                        throw m_entry.path().wstring().c_str();
                    image_pool[m_entry.path().stem().string()] = image;
                }
            }
            else if (filePath.filename() == "enemy")
            {
                for (const auto& m_entry : fs::directory_iterator(filePath))
                {
                    Atlas* atlas = new Atlas();
                    atlas->load(m_entry.path());
                    for (int i = 0; i < atlas->get_size(); i++)
                    {
                        IMAGE* image = atlas->get_image(i);
                        if (!check_image_valid(image))
                            throw m_entry.path().wstring().c_str();
                    }
                    
                    atlas_pool[m_entry.path().filename().string()] = atlas;
                }
            }
            
        }
    }