Reputation: 1008
I want to check if a boost::filesystem::path
provided by a user is actually a file which is createable. Createable means: the directory in front of the filename exists.
std::string input = ~/some/dir/file.xyz
boost::filesystem::path path(input);
if (~/some/dir is a directory) // <-- How to get this?
return true;
What's a solution with boost::filesystem
? Or maybe there are better tools?
Upvotes: 2
Views: 1222
Reputation: 38919
First off, c++17 has added filesystem
to the standard. So, yeah you should use that. Prior to c++17 I'd still go with something like: experimental/filesystem
Once you have filesystem::path path
defined, you can use parent_path
which:
Returns the
path
to the parent directory
You can use this path
in the filesystem::exists
which:
Checks if the given file status or
path
corresponds to an existing file or directory
So the condition in your if
-statement should look something like this:
filesystem::exists(path.parent_path())
Upvotes: 3