C++:Boost:如何检查我的工作目录中另一个文件夹中是否存在一个文件夹?

标签 c++ boost filesystems

代码:

boost::filesystem::path config_folder(Config::CONFIG_FOLDER_NAME);

if( !(boost::filesystem::exists(config_folder)))
{
    std::cout << "Network Config Directory not found...\n";
    std::cout << "Creating folder called " << Config::CONFIG_FOLDER_NAME << "\n";
    boost::filesystem::create_directory(config_folder);
}

if( !(boost::filesystem::exists(boost::format("%s/%s") % config_folder % Config::fmap[Config::current_hash_function]));
{
    std::cout << "This hash has not been configure before...\n";
    std::cout << "Creating folder called " << Config::fmap[Config::current_hash_function] << "\n";
    boost::filesystem::create_directory(boost::format("%s/%s") % config_folder % Config::fmap[Config::current_hash_function]);
}

因此,首先,如果配置文件夹不存在,请创建它。 (这部分有效) 接下来,检查 current_hash_function 文件夹是否存在,如果不存在,请创建它。 (这部分不起作用)

我遇到的错误;

src/neural_networks/shared.cpp:41: error: no matching function for call to ‘exists(boost::basic_format<char, std::char_traits<char>, std::allocator<char> >&)’

我在 fs:exists 检查中使用 boost 格式的原因是因为我不知道如何创建 2 层深的路径

最佳答案

The / operator重载以连接 path 对象。无需显式字符串格式化,也无需担心特定于平台的路径分隔符。

if(!(boost::filesystem::exists(config_folder / Config::fmap[Config::current_hash_function]));

任何一个操作数都可以是 std::string,只要另一个是 path

关于C++:Boost:如何检查我的工作目录中另一个文件夹中是否存在一个文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5981947/

相关文章:

c++ - 存储和缓存的选择

c++ - 标准头文件中的错误

c++ - 添加大数字 C++ - 初始化程序失败

c++ - 为模板类制作 makefile

c++ - 如何删除 boost 图的子图

c++ - 如何使用 Boost 实现测试套件和用例的组织?

c++ - 在 Boost Spirit 中解析嵌套键值对

创建文件系统,需要 C 的建议

linux - 我如何在 python 中获得 unix 中的最大文件系统路径长度?

dialog - Electron dial.showOpenDialog 未运行回调