filesystem error: cannot create directory: No such file or directory

filesystem error: cannot create directory: No such file or directory

我不敢相信似乎没有人发布此错误或解决方案。我正在使用 C++ 17。

我正在尝试 运行 以下代码。

fs::directory_entry result(CodeSource::ARDUINO_SOURCE);
if (!result.exists()) {
    fs:create_directory(result);
}
return result;

我得到以下结果。

terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot create directory: No such file or directory [~/arduino_source]

变量明明是“~/arduino_source”当然不存在。这就是我创建它的原因。

文档说它会像我在上面 运行 mkdir 一样执行,所以我试了一下,它工作得很好。

我是运行宁Ubuntu20.10.

我写了一个最小程序,确认是路径~/arduino_source引起的,需要绝对路径或者相对路径,但是对于波浪号~字符,会导致异常.

由于 API directory_entry::directory_entrystd::filesystem::create_directory 都可以抛出,您的代码需要处理异常以使其健壮。