C++:Boost:需要目录导航逻辑的帮助

发布于 2024-11-05 16:58:46 字数 1131 浏览 5 评论 0原文

因此,我尝试更改目录来保存文件,然后改回之前所在的目录。

本质上:

cd folder_name
<save file>
cd ../

这是我到目前为止的代码:

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_folder, 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);

}

目前,每次调用该方法时都会发生这种情况:
文件夹不存在:已创建
文件夹不存在:已创建

它不执行 cd ../ 部分。 =(

所以我的目录结构如下所示:

folder_name
- 文件夹名称
-- 文件夹名称
--- 文件夹名称

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_folder, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲影 2024-11-12 16:58:46

根据文档, current_path 方法有点危险,因为它可能会同时被其他程序修改。

因此,从 CONFIG_FOLDER_NAME 进行操作可能会更好。

您可以将更大的路径名传递给 fann_save 吗?类似的东西:

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());

否则,如果您对使用 current_path 感到满意或无法在 fann_save 中使用更大的路径,我会尝试类似的方法:

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

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);
煮酒 2024-11-12 16:58:46

您可以尝试使用此代码吗?

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文