我想检查用户提供的 boost::filesystem::path
是否实际上是一个可创建的文件。可创建表示:文件名前面的目录存在。
std::string input = ~/some/dir/file.xyz
boost::filesystem::path path(input);
if (~/some/dir is a directory) // <-- How to get this?
return true;
boost::filesystem
的解决方案是什么?或者也许有更好的工具?
最佳答案
首先,c++17已添加filesystem
达到标准。所以,是的,你应该使用它。之前c++17我仍然会选择类似的东西:experimental/filesystem
定义filesystem::path path
后,您可以使用 parent_path
其中:
Returns the
path
to the parent directory
您可以在 filesystem::exists
中使用此路径
其中:
Checks if the given file status or
path
corresponds to an existing file or directory
因此 if
语句中的条件应如下所示:
filesystem::exists(path.parent_path())
关于c++ - 如何使用 boost::filesystem 检查路径是否是可创建文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42127450/