c++ - 如何使用 boost::filesystem 检查路径是否是可创建文件?

标签 c++ boost boost-filesystem

我想检查用户提供的 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 的解决方案是什么?或者也许有更好的工具?

最佳答案

首先,已添加filesystem达到标准。所以,是的,你应该使用它。之前我仍然会选择类似的东西: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/

相关文章:

c++ - 可以处理随机访问和 key 搜索的数据结构是什么?

c++ - SDL_GetKeyboardState 不返回按键的当前状态

C++ - 如何使用 reference_wrapper vector

c++ - 语义 Action 在 boost::spirit 解析中运行多次

c++ - boost::archive::text_iarchive 构造函数异常

c++ - 具有不同类型和操作可供选择的 C++ 计算器

c++ - boost::program_options bool_switch 使用多次

c++ - 为什么 vector 总是空的?

C++ - 如何以平台无关、线程安全的方式将文件的上次修改日期和时间格式化为用户首选的日期/时间区域设置格式

C++:比较 boost::filesystem 中的路径时如何忽略第一个目录路径?