3

したがってboost::filesystem::path Base、フォルダーが存在しない場合はフォルダーを作成し、文字列からバイナリファイルを作成するベースがあります。現在、私は次のような機能を持っています:

void file_service::save_string_into_file( std::string contents, std::string name )
{
    std::ofstream datFile;
    name = "./basePath/extraPath/" + name;
    datFile.open(name.c_str(), std::ofstream::binary | std::ofstream::trunc | std::ofstream::out    );
    datFile.write(contents.c_str(), contents.length());
    datFile.close();
}

ディレクトリから存在する必要があります。では、目的の機能を実現するために、関数を boost.filesystem API に更新する方法を知りたいですか?

4

2 に答える 2

6

boost::filesystem ライブラリを使用するには、コンパイル済みの boost::filesystem 静的ライブラリと boost::system 静的ライブラリに対してリンクする必要があることに注意してください。

#include "boost/filesystem.hpp"

boost::filesystem::path rootPath ( "./basePath/extraPath/" );
boost::system::error_code returnedError;

boost::filesystem::create_directories( rootPath, returnedError );

if ( returnedError )
   //did not successfully create directories
else
   //directories successfully created
于 2011-08-06T18:08:41.970 に答える
4

にはcreate_directories便利な機能がありboost::filesystemます。ディレクトリを再帰的に作成するため、新しいパスを自分でトラバースする必要はありません。

です<boost/filesystem/convenience.hpp>

于 2011-08-06T17:05:15.250 に答える