Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::filesystem::remove,std::filesystem::remove_all

      From cppreference.com
      <cpp‎ |filesystem
       
       
      Filesystem library
      Classes
      Functions
      File types
       
      Defined in header<filesystem>
      bool remove(conststd::filesystem::path& p);
      (1)(since C++17)
      bool remove(conststd::filesystem::path& p,std::error_code& ec)noexcept;
      (2)(since C++17)
      (3)(since C++17)
      (4)(since C++17)
      1,2) The file or empty directory identified by the pathp is deleted as if by the POSIXremove. Symlinks are not followed (symlink is removed, not its target).
      3,4) Deletes the contents ofp (if it is a directory) and the contents of all its subdirectories, recursively, then deletesp itself as if by repeatedly applying the POSIXremove. Symlinks are not followed (symlink is removed, not its target).

      Contents

      [edit]Parameters

      p - path to delete
      ec - out-parameter for error reporting in the non-throwing overload.

      [edit]Return value

      1,2)true if the file was deleted,false if it did not exist. The overload that takeserror_code& argument returnsfalse on errors.
      3,4) Returns the number of files and directories that were deleted (which may be zero ifp did not exist to begin with). The overload that takeserror_code& argument returnsstatic_cast<std::uintmax_t>(-1) on error.

      [edit]Exceptions

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

      1,3) 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,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

      On POSIX systems, this function typically callsunlink andrmdir as needed, on WindowsDeleteFileW andRemoveDirectoryW.

      Ifp did not exist, this function returnsfalse and does not report an error.

      [edit]Example

      Run this code
      #include <cstdint>#include <filesystem>#include <fstream>#include <iostream> int main(){namespace fs= std::filesystem;std::cout<<std::boolalpha;     fs::path tmp{std::filesystem::temp_directory_path()}; constauto O_O{"O_O"};std::ofstream{tmp/ O_O}<< O_O;// creates file containing O_Ostd::cout<<"remove(): "<< fs::remove(tmp/ O_O)<<'\n';// successstd::cout<<"remove(): "<< fs::remove(tmp/ O_O)<<'\n';// fail std::filesystem::create_directories(tmp/"abcdef/example");conststd::uintmax_t n{fs::remove_all(tmp/"abcdef")};std::cout<<"remove_all(): "<< n<<" files or directories\n";}

      Possible output:

      remove(): trueremove(): falseremove_all(): 2 files or directories

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 3014C++17error_code overload ofremove_all marked noexcept but can allocate memorynoexcept removed

      [edit]See also

      erases a file
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/filesystem/remove&oldid=178128"

      [8]ページ先頭

      ©2009-2025 Movatter.jp