| Defined in header <experimental/filesystem>
|
||
| bool create_directory( const path& p ); bool create_directory( const path& p, error_code& ec ); |
(1) | (filesystem TS) |
| bool create_directory( const path& p, const path& existing_p ); bool create_directory( const path& p, const path& existing_p, error_code& ec ); |
(2) | (filesystem TS) |
| bool create_directories( const path& p ); bool create_directories( const path& p, error_code& ec ); |
(3) | (filesystem TS) |
p as if by POSIX mkdir() with a second argument of static_cast<int>(fs::perms::all) (the parent directory must already exist). If p already exists and is already a directory, the function does nothing (this condition is not treated as an error). existing_p (which must be a directory that exists). It is OS-dependent which attributes are copied: on POSIX systems, the attributes are copied as if by
stat(existing_p.c_str(), &attributes_stat)
mkdir(p.c_str(), attributes_stat.st_mode)CreateDirectoryExW(existing_p.c_str(), p.c_str(), 0)p that does not already exist.The non-throwing overloads return false if any error occurs.
Parameters
| p | - | the path to the new directory to create |
| existing_p | - | the path to a directory to copy the attributes from |
| ec | - | out-parameter for error reporting in the non-throwing overload |
Return value
true if directory creation is successful, false otherwise.Exceptions
error_code& parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. The overload taking an error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. This overload has noexcept specification: error_code& parameter throws filesystem_error on underlying OS API errors, constructed with p as the first argument, existing_p as the second argument, and the OS error code as the error code argument. std::bad_alloc may be thrown if memory allocation fails. The overload taking an error_code& parameter sets it to the OS API error code if an OS API call fails, and executes ec.clear() if no errors occur. This overload has noexcept specification: Notes
The attribute-preserving overload (2) is implicitly invoked by copy() when recursively copying directories. Its equivalent in boost.filesystem is copy_directory (with argument order reversed).
Example
#include <cstdlib>
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
fs::create_directories("sandbox/1/2/a");
fs::create_directory("sandbox/1/2/b");
fs::permissions("sandbox/1/2/b", fs::perms::remove_perms | fs::perms::others_all);
fs::create_directory("sandbox/1/2/c", "sandbox/1/2/b");
std::system("ls -l sandbox/1/2");
fs::remove_all("sandbox");
}drwxr-xr-x 2 user group 4096 Apr 15 09:33 a
drwxr-x--- 2 user group 4096 Apr 15 09:33 b
drwxr-x--- 2 user group 4096 Apr 15 09:33 cSee also
| creates a symbolic link (function) | |
| copies files or directories (function) | |
| identifies file system permissions (enum) |
Content from this page on cppreference.com, licensed under CC-BY-SA 3.0 and GFDL. Modified: restyled, site navigation removed, code windows added. Offline snapshot of 9 February 2025.