1

そのため、ディレクトリを変更してファイルを保存し、以前のディレクトリに戻そうとしています。

基本的に:

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 ../。=(

したがって、私のディレクトリ構造は次のようになります。

フォルダ名
- フォルダ名
-- フォルダ名
--- フォルダ名

4

2 に答える 2

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);
于 2011-05-09T06:54:57.950 に答える
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);
}
于 2011-05-09T06:49:58.517 に答える