C++:Boost:需要目录导航逻辑的帮助
因此,我尝试更改目录来保存文件,然后改回之前所在的目录。
本质上:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据文档, current_path 方法有点危险,因为它可能会同时被其他程序修改。
因此,从 CONFIG_FOLDER_NAME 进行操作可能会更好。
您可以将更大的路径名传递给 fann_save 吗?类似的东西:
否则,如果您对使用 current_path 感到满意或无法在 fann_save 中使用更大的路径,我会尝试类似的方法:
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:
otherwise if you are happy with using current_path or cant use a larger path in fann_save, i would try something like:
您可以尝试使用此代码吗?
Can you try with this code instead.