7

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

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • 1
    To create paths more than one level deep, use [`create_directories`](http://www.boost.org/doc/libs/1_41_0/libs/filesystem/doc/reference.html#Convenience-functions). – Rob Kennedy May 12 '11 at 17:54

2 Answers2

10

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.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • yay this worked. But... I call the function that has this code several times, and every time it's saying that the hash folder (the inner one) is never there, and contiunally says it is re-creating it. but no data is lost on the filesystem... =\ – NullVoxPopuli May 12 '11 at 18:06
  • Yep. In programming, as in life, solving one problem often just exposes another. Check whether the directory is really being created. Check that it's being created in the location you expect. If you're on Windows, beware of path virtualization. Make sure `fmap` contains what you think it does. Good luck. – Rob Kennedy May 12 '11 at 18:23
  • Yup, all that is as I want. It's just the if is failing, I think. =\ – NullVoxPopuli May 12 '11 at 18:28
0

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])))
Cubbi
  • 46,567
  • 13
  • 103
  • 169