所以,我正在尝试更改我的目录以保存文件,然后更改回我以前的目录。
本质:
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
- folder_name
- folder_name
--- folder_name
答案 0 :(得分:1)
根据文档,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);
答案 1 :(得分:0)
您可以尝试使用此代码。
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);
}