user1587451
user1587451

Reputation: 1008

How to check with boost::filesystem if a path is a creatable file?

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

Answers (1)

Jonathan Mee
Jonathan Mee

Reputation: 38919

First off, has added filesystem to the standard. So, yeah you should use that. Prior to 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

Related Questions