3

後でフォルダーを作成するために現在のパスが必要な C++ プログラムがあります。私の実行可能ファイルの場所は、たとえば/home/me/foo/bin. これは私が実行するものです:

//Here I expect '/home/me/foo/bin/', but get ''
auto currentPath = boost::filesystem::current_path(); 

//Here I expect '/home/me/foo/', but get ''
auto parentPath = currentPath.parent_path();

//Here I expect '/home/me/foo/foo2/', but get 'foo2/'
string subFolder    = "foo2";
string folderPath   = parentPath.string() + "/" + subFolder + "/";

//Here I expect to create '/home/me/foo/foo2/', but get a core dump
boost::filesystem::path boostPath{ folderPath};
boost::filesystem::create_directories( boostPath); 

パッケージマネージャーConanでインストールされたBoost 1.66を使用して、Ubuntu 16.04で実行しています。

Conanを使用せずに、以前のバージョンのBoost(1.45だと思います)でこれを正常に実行していました。Boostは私のマシンに正常にインストールされました。を実行するとコア ダンプが表示されるようになりましたcreate_directories( boostPath);

2 つの質問:

  1. current_path()実際のパスを提供せず、代わりにリターンと空のパスを提供しないのはなぜですか?
  2. current_path() が何も返さなかったとしても、なぜ実行してもコア ダンプが残るのでしょうsudoか? ルートにフォルダを作成するだけではないでしょうか?

編集:

コンパイルされたプログラムを実行し、coutデバッグ モードを使用するのではなく、行間に上記の変数の出力をいくつか入れると、通常、次の出力が得られます。

currentPath: ""
parentPath: ""
folderPath: /foo2/
Segmentation fault (core dumped)

しかし、時々 (時間の約 20%)、次の出力が得られます。

currentPath: "/"
parentPath: "/home/me/fooA�[oFw�[oFw@"
folderPath: /home/me/fooA�[oFw�[oFw@/foo2/
terminate called after throwing an instance of 'boost::filesystem::filesystem_error'
  what():  boost::filesystem::create_directories: Invalid argument
Aborted (core dumped)

編集2:

実行すると、次のconan profile show defaultようになります。

[settings]
os=Linux
os_build=Linux
arch=x86_64
arch_build=x86_64
compiler=gcc
compiler.version=5
compiler.libcxx=libstdc++
build_type=Release
[options]
[build_requires]
[env]
4

1 に答える 1