c++17--filesystem

c++17的新特性,所以编译时需要指定C++标准,和编译参数:

$ g++ -std=c++17 main.cpp -o myprogram -lstdc++fs

cmake:
set(CMAKE_CXX_STANDARD 17)#或者可以设置更高的版本,因为C++的版本都是向后兼容的
set(CMAKE_CXX_FLAGS  "-std=c++17")

std::filesystem

文件系统库,本质上就是一个命名空间。

链接

链接

path

这是filesysytem命名空间内的一个类。

成员函数

链接

可能存在翻译不正确的问题,看代码实例。

从path中获取字符串路径---string()

可以使用 `filesystem::path` 类的 `string()` 成员函数来将路径转换为 `std::string` 类型。例如:

```cpp
#include <iostream>
#include <filesystem>

int main() {
  std::filesystem::path path = std::filesystem::current_path();
  std::string path_str = path.string();
  std::cout << "Current path is " << path_str << '\n';
  return 0;
}

std::filesystem::path::preferred_separator---可以获取当前操作系统的路径分隔符。

#include <iostream>
#include <filesystem>

int main(){
	std::string current_p=std::filesystem::current_path().string()+std::filesystem::path::preferred_separator+"new_dir";
	std::cout<<current_p<<std::endl;
	return 0;
}

判断路径是否存在,创建路径

cmake:
cmake_minimum_required(VERSION 3.9.0)
project(pro_judge_path)

set(CMAKE_CXX_STANDARD 20)
#set(CMAKE_CXX_FLAGS "-lstdc++fs")

add_executable(exec_file main.cpp)
#include <iostream>
#include <filesystem>

int main() {
    std::string path_str = "./path/to/some/directory";

    // 判断路径是否存在
    std::filesystem::path path_obj(path_str);
    if (std::filesystem::is_directory(path_obj)) {
        std::cout << "该路径已存在" << std::endl;
    } else {
        std::cout << "该路径不存在" << std::endl;
        // 创建路径
        if (std::filesystem::create_directories(path_obj)) {
            std::cout << "路径创建成功" << std::endl;
        } else {
            std::cout << "路径创建失败" << std::endl;
        }
    }

    return 0;
}

filesystem判断文件是否存在,创建文件

判断文件是否存在

可以直接传递字符串。

#include <iostream>
#include <filesystem>

int main(){
  if(std::filesystem::exists("test1.h")){
    std::cout<<"yes"<<std::endl;
  }

  return 0;
}

#include <fstream>
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main() {
    std::string file_path = "file.txt";
    fs::path path_obj(file_path);
 
    if (fs::exists(path_obj)) {
        std::cout << "文件: " << file_path << " 已存在" << std::endl;
    } else {
        std::ofstream outfile(file_path); // 创建文件
        if (outfile.is_open()) {
            outfile << "Hello World!" << std::endl;
            outfile.close();
            std::cout << "文件 " << file_path << " 创建成功" << std::endl;
        } else {
            std::cout << "文件 " << file_path << " 创建失败" << std::endl;
        }
    }
    return 0;
}
 


在这个示例中,我们首先将要创建的文件路径转换为 std::filesystem::path 对象,然后使用 std::filesystem::exists() 函数来判断该文件是否已存在。如果文件已存在,则输出相应的提示信息;否则,使用 std::ofstream::open() 函数创建文件,并向其中写入一些内容。如果创建文件成功,则输出相应的提示信息,否则输出另一种提示信息。

需要注意的是,在使用 std::filesystem 库时,需要在编译时加入 `-lstdc++fs` 参数,或者在 CMakeLists.txt 中设置 `set(CMAKE_CXX_FLAGS "-lstdc++fs")`,以引入与文件系统相关的库;并且,需要包含 `<fstream>` 头文件,才能使用 ofstream 类和其相关的函数。

fstream

filesystem获取当前路径

 C++17 引入的 `std::filesystem` 标准库中的 `current_path()` 函数来获取当前路径。它返回一个 `std::filesystem::path` 类型的对象,表示当前路径。

#include <iostream>
#include <filesystem>

int main(){
    std::cout<<"currrent path: "<<std::filesystem::current_path()<<std::endl;
    return 0;
}

filesystem指定目录下的所有子目录

迭代所有子目录--filesystem::recursive_directory_iterator(root_path)

include <iostream>
#include <filesystem>

namespace fs=std::filesystem;

void list_all_subdirectories(const fs::path& root_path) {
    for (const auto& entry : fs::recursive_directory_iterator(root_path)) {
        if (entry.is_directory()) {
            std::cout << entry.path() << std::endl;
        }
    }
}

int main() {
    fs::path root_path("/home/wjy/workspace");
    list_all_subdirectories(root_path);
    return 0;
}

获取第一级子目录---filesystem::directory_iterator(root_path)

#include <iostream>
#include <filesystem>

namespace fs=std::filesystem;

void list_all_subdirectories(const fs::path& root_path) {
    for (const auto& entry : fs::directory_iterator(root_path)) {
        if (entry.is_directory()) {
            std::cout << entry.path() << std::endl;
        }
    }
}

int main() {
    fs::path root_path("/home/wjy/workspace");
    list_all_subdirectories(root_path);
    return 0;
}

获取不包含路径的文件名或者目录名

int main() {
    std::vector<std::string> package_names;
    for(const auto& sub_dir:std::filesystem::directory_iterator("./")){
        if(sub_dir.is_directory()){
            package_names.push_back(sub_dir.path().filename().string());
        }
    }
    for(std::string path:package_names) std::cout<<path<<std::endl;
    return 0;
}

filesystem::filesystem_error

如果fiilesystem的函数错误,可以捕获filesystem_error的错误,并且输出原因。

#include <iostream>
#include <filesystem>

namespace fs=std::filesystem;

int main(){
    std::string my_path="/home/test_dir";

    try {
       fs::create_directories(my_path);
    } catch (const std::filesystem::filesystem_error &e) {
       std::cout << "创建目录时发生错误:" << e.what() << std::endl;
    }
    return 0;
}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值