Search code examples
c++pathmkdirstd-filesystem

Emulating mkdir -p with the C++ standard library


This question: mkdir c++ function tells us how to create a directory programmatically in C++, introducing us to std::filesystem::create - which acts similarly to the mkdir command in Unix shells.

However, this does not seem to allow for recursively creating the entire path: mkdir -p /path/to/nonexisting/dir/and/then/some. Is there some facility in the filesystem library for doing that? Or - should we just loop over the path elements, checking for existence and writability as necessary?


Solution

  • What you are looking for is std::filesystem::create_directories() for which you can find more detailed information in the reference.