我有一个 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);
我在 Ubuntu 16.04 上运行,使用与包管理器 Conan 一起安装的 Boost 1.66。
我曾经在不使用柯南的情况下使用以前版本的 Boost(我相信是 1.45)成功运行此程序。Boost 通常安装在我的机器上。我现在在运行时得到一个核心转储create_directories( boostPath);
。
两个问题:
- 为什么不
current_path()
为我提供实际路径,而是返回和空路径? - 即使 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]