开发者

C++: Boost: Need help with directory navigation logic

开发者 https://www.devze.com 2023-03-04 01:06 出处:网络
So, I\'m trying to change my directory to save files, and then change back to the directory I was previously in.

So, I'm trying to change my directory to save files, and then change back to the directory I was previously in.

Essentially:

cd folder_name
<save file>
cd ../

Here is the code I have so far:

void save_to_folder(struct fann * network, const char * save_name)
{
    boost::filesystem::path config_folder(Config::CONFIG_FOLDER_NAME);
    boost::filesystem::path parent_folder("../");


    if( !(boost::filesystem::equivalent(config_folde开发者_开发技巧r, boost::filesystem::current_path())))
        {
            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);
            }
            boost::filesystem::current_path(config_folder);
        }

    fann_save(network, save_name);
    boost::filesystem::current_path(parent_folder);

}

Currently, what is happening is this every time the method is called:

Folder doesn't exist: gets created

Folder doesn't exist: gets created

It's not doing the cd ../ part. =(

so my directory structure looks like this:

folder_name

- folder_name

-- folder_name

--- folder_name


According to the docs, the current_path method is a little dangerous because it might be modified by other programs at the same time.

So it would probably be better to operate from the CONFIG_FOLDER_NAME.

Can you pass a larger path name to fann_save? something like:

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);
}
fann_save(network, (boost::format("%s/%s") % config_folder % save_name).str().c_str());

otherwise if you are happy with using current_path or cant use a larger path in fann_save, i would try something like:

boost::filesystem::path up_folder((boost::format("%s/..") % Config::CONFIG_FOLDER_NAME).str());
boost::filesystem::current_path(up_folder);


Can you try with this code instead.

void save_to_folder(struct fann * network, const char * save_name)
{
    boost::filesystem::path configPath(boost::filesystem::current_path() / Config::CONFIG_FOLDER_NAME);

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号