现代C++ 文件系统库

发布于:2025-07-01 ⋅ 阅读:(12) ⋅ 点赞:(0)
一、std::filesystem 的前世今生

C++11 之前,文件系统操作依赖于平台特定的 API(如 Windows 的CreateFile或 POSIX 的open),缺乏统一接口。C++17 正式将std::filesystem纳入标准库,该库最初由 Boost.Filesystem 演化而来,提供了跨平台的文件系统操作能力。

核心优势

  • 跨平台兼容性:一次编写,支持 Windows、Linux、macOS 等主流平台
  • RAII 设计:通过pathdirectory_iterator等类实现资源自动管理
  • 性能优化:底层封装系统 API,减少不必要的开销
// 编译时需添加编译选项:-std=c++17
#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main() {
    std::cout << "C++ filesystem version: " << fs::version() << std::endl;
    return 0;
}
二、路径操作:从字符串到 path 对象的蜕变
1. 路径构造与标准化

传统 C++ 中使用字符串拼接路径容易出错(如反斜杠转义),fs::path提供了安全的路径处理方式:

void path_operations() {
    // 自动适配平台分隔符
    fs::path p1 = "data/images/logo.png";
    fs::path p2 = "/usr/local/include/c++/11";
    
    // 标准化路径(消除.和..)
    fs::path p3 = "/home/user/../documents/./project";
    std::cout << "Normalized: " << p3.normalize() << std::endl; // 输出:/home/documents/project
    
    // 跨平台路径拼接
    fs::path base = "/app";
    fs::path sub = "data/logs";
    std::cout << "Combined: " << base / sub << std::endl; // 输出:/app/data/logs
}
2. 路径组件分解
void path_components() {
    fs::path p = "/user/documents/report.pdf";
    
    std::cout << "Filename: " << p.filename() << std::endl;       // report.pdf
    std::cout << "Stem: " << p.stem() << std::endl;           // report
    std::cout << "Extension: " << p.extension() << std::endl;   // .pdf
    std::cout << "Parent path: " << p.parent_path() << std::endl; // /user/documents
    
    // 遍历路径层级
    std::cout << "Path components: ";
    for (const auto& component : p) {
        std::cout << component << "/"; // 输出:user/documents/report.pdf/
    }
}
三、文件与目录操作:从属性查询到内容修改
1. 文件属性查询
void file_attributes() {
    fs::path file = "example.txt";
    
    // 基础属性
    if (fs::exists(file)) {
        std::cout << "File size: " << fs::file_size(file) << " bytes" << std::endl;
        std::cout << "Last write time: " << fs::last_write_time(file) << std::endl;
        
        // 类型判断
        if (fs::is_regular_file(file)) {
            std::cout << "It's a regular file" << std::endl;
        } else if (fs::is_directory(file)) {
            std::cout << "It's a directory" << std::endl;
        }
    }
}
2. 目录遍历与文件操作
void directory_operations() {
    fs::path dir = "src";
    
    // 遍历目录(C++17范围for)
    std::cout << "Files in " << dir << ":\n";
    for (const auto& entry : fs::directory_iterator(dir)) {
        std::cout << "  - " << entry.path() << std::endl;
        
        // 重命名文件
        if (entry.path().extension() == ".cpp") {
            fs::path new_name = entry.path().stem() + "_new" + entry.path().extension();
            fs::rename(entry.path(), new_name);
        }
    }
    
    // 创建多级目录
    fs::create_directories("build/intermediate/output");
    
    // 复制文件
    fs::copy_file("main.cpp", "backup/main.cpp.bak");
    
    // 删除文件(谨慎使用)
    // fs::remove("temp.txt");
}
四、文件搜索与内容统计
1. 递归搜索特定类型文件
void search_files(const fs::path& dir, const std::string& ext, std::vector<fs::path>& results) {
    try {
        for (const auto& entry : fs::directory_iterator(dir)) {
            if (fs::is_directory(entry)) {
                search_files(entry.path(), ext, results); // 递归搜索子目录
            } else if (entry.path().extension() == ext) {
                results.push_back(entry.path());
            }
        }
    } catch (const fs::filesystem_error& e) {
        std::cerr << "Error: " << e.what() << std::endl;
    }
}

int main() {
    std::vector<fs::path> cpp_files;
    search_files("project", ".cpp", cpp_files);
    
    std::cout << "Found " << cpp_files.size() << " C++ files:\n";
    for (const auto& p : cpp_files) {
        std::cout << "  - " << p << std::endl;
    }
    return 0;
}
2. 目录大小统计
uintmax_t calculate_directory_size(const fs::path& dir) {
    uintmax_t size = 0;
    for (const auto& entry : fs::recursive_directory_iterator(dir)) {
        if (fs::is_regular_file(entry)) {
            size += fs::file_size(entry);
        }
    }
    return size;
}

void print_size_human_readable(uintmax_t bytes) {
    if (bytes < 1024) {
        std::cout << bytes << " B";
    } else if (bytes < 1024 * 1024) {
        std::cout << bytes / 1024.0 << " KB";
    } else {
        std::cout << bytes / (1024.0 * 1024) << " MB";
    }
}
五、性能优化
  1. 避免不必要的递归
    • 使用recursive_directory_iterator时,可通过skip_directory()跳过特定目录
fs::recursive_directory_iterator it(dir);
it.skip_directory(); // 跳过当前目录
  1. 缓存路径状态
    • 频繁查询属性时,使用fs::file_status缓存结果
fs::file_status status = fs::status(file);
if (fs::is_regular_file(status)) { /*...*/ }

  1. 错误处理策略
    • 操作文件系统时,始终捕获fs::filesystem_error异常
try {
    fs::remove_all("temp");
} catch (const fs::filesystem_error& e) {
    std::cerr << "Remove error: " << e.path1() << " - " << e.what() << std::endl;
}
六、跨平台注意事项
  1. 路径分隔符

    • Windows 使用反斜杠\,POSIX 使用正斜杠/fs::path会自动适配
  2. 权限管理

    • Windows 和 POSIX 的权限模型不同,需注意fs::permissions的平台差异
  3. 符号链接处理

    • Linux 中符号链接常见,可通过fs::is_symlink()判断并使用fs::read_symlink()解析
总结

std::filesystem库的引入大幅简化了 C++ 文件系统操作,通过 RAII 设计和跨平台抽象,让开发者无需关心底层差异。


网站公告

今日签到

点亮在社区的每一天
去签到