C++: Boost: how do I check the existence of a folder inside another folder in my working directory?

匿名 (未验证) 提交于 2019-12-03 08:44:33

问题:

code:

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]); }

So, first, if the config folder doesn't exist, create that. (this part works) Next, check if the current_hash_function folder exists, if not.. create it. (this part doesn't work)

The error I'm getting;

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> >&)’

reason why I did the boost format in the fs:exists check, is because I don't how to create a path 2 levels deep

回答1:

The / operator is overloaded to concatenate path objects. No need for explicit string formatting, and no need to worry about the platform-specific path-separator character, either.

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

Either operand can be a std::string as long as the other is a path.



回答2:

boost::filesystem::exists() needs an argument of type boost::filesystem::path or something that is implicitly-convertible to it, such as std::string, but what you're passing is neither of those.

the following should work:

if( !( boost::filesystem::exists( str(                boost::format("%s/%s")                % config_folder % Config::fmap[Config::current_hash_function])))


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!