Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::create_directory,std::filesystem::create_directories

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      bool create_directory(conststd::filesystem::path& p);
      (1)(since C++17)
      bool create_directory(conststd::filesystem::path& p,std::error_code& ec)noexcept;
      (2)(since C++17)
      bool create_directory(conststd::filesystem::path& p,
                             conststd::filesystem::path& existing_p);
      (3)(since C++17)
      bool create_directory(conststd::filesystem::path& p,

                             conststd::filesystem::path& existing_p,

                             std::error_code& ec)noexcept;
      (4)(since C++17)
      bool create_directories(conststd::filesystem::path& p);
      (5)(since C++17)
      bool create_directories(conststd::filesystem::path& p,std::error_code& ec);
      (6)(since C++17)
      1,2) Creates the directoryp as if by POSIXmkdir() with a second argument ofstatic_cast<int>(std::filesystem::perms::all) (the parent directory must already exist). If the function fails becausep resolves to an existing directory, no error is reported. Otherwise on failure an error is reported.
      3,4) Same as(1,2), except that the attributes of the new directory are copied fromexisting_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)
      On Windows OS, no attributes ofexisting_p are copied.
      5,6) Executes(1,2) for every element ofp that does not already exist. Ifp already exists, the function does nothing (this condition is not treated as an error).

      Contents

      [edit]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

      [edit]Return value

      true if a directory was newly created for the directoryp resolves to,false otherwise.

      [edit]Exceptions

      Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.

      1,5) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument and the OS error code as the error code argument.
      2,6) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur.
      3) Throwsstd::filesystem::filesystem_error on underlying OS API errors, constructed withp as the first path argument,existing_p as the second path argument, and the OS error code as the error code argument.
      4) Sets astd::error_code& parameter to the OS API error code if an OS API call fails, and executesec.clear() if no errors occur.

      [edit]Notes

      The attribute-preserving overload(3,4) is implicitly invoked bycopy() when recursively copying directories. Its equivalent in boost.filesystem iscopy_directory (with argument order reversed).

      [edit]Example

      Run this code
      #include <cassert>#include <cstdlib>#include <filesystem> int main(){std::filesystem::current_path(std::filesystem::temp_directory_path()); // Basic usage    std::filesystem::create_directories("sandbox/1/2/a");    std::filesystem::create_directory("sandbox/1/2/b"); // Directory already exists (false returned, no error)assert(!std::filesystem::create_directory("sandbox/1/2/b")); // Permissions copying usagestd::filesystem::permissions("sandbox/1/2/b",std::filesystem::perms::others_all,        std::filesystem::perm_options::remove);    std::filesystem::create_directory("sandbox/1/2/c","sandbox/1/2/b"); std::system("ls -l sandbox/1/2");std::system("tree sandbox");std::filesystem::remove_all("sandbox");}

      Possible output:

      drwxr-xr-x 2 user group 4096 Apr 15 09:33 adrwxr-x--- 2 user group 4096 Apr 15 09:33 bdrwxr-x--- 2 user group 4096 Apr 15 09:33 csandbox└── 1    └── 2        ├── a        ├── b        └── c

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2935C++17error if target already exists but is not a directorynot error
      LWG 3014C++17error_code overload ofcreate_directories marked noexcept but can allocate memorynoexcept removed
      P1164R1C++17creation failure caused by an existing non-directory file is not an errormade error

      [edit]See also

      creates a symbolic link
      (function)[edit]
      (C++17)
      copies files or directories
      (function)[edit]
      (C++17)
      identifies file system permissions
      (enum)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/create_directory&oldid=171249"

      [8]ページ先頭

      ©2009-2025 Movatter.jp