| Classes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| File types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <filesystem> | ||
void rename(conststd::filesystem::path& old_p, conststd::filesystem::path& new_p); | (1) | (since C++17) |
void rename(conststd::filesystem::path& old_p, conststd::filesystem::path& new_p, | (2) | (since C++17) |
Moves or renames the filesystem object identified byold_p tonew_p as if by the POSIXrename:
Rename fails if
Contents |
| old_p | - | path to move or rename |
| new_p | - | target path for the move/rename operation |
| ec | - | out-parameter for error reporting in the non-throwing overload |
(none)
Any overload not markednoexcept may throwstd::bad_alloc if memory allocation fails.
#include <filesystem>#include <fstream>namespace fs= std::filesystem; int main(){std::filesystem::path p=std::filesystem::current_path()/"sandbox";std::filesystem::create_directories(p/"from");std::ofstream{ p/"from/file1.txt"}.put('a');std::filesystem::create_directory(p/"to"); // fs::rename(p / "from/file1.txt", p / "to/"); // error: "to" is a directory fs::rename(p/"from/file1.txt", p/"to/file2.txt");// OK// fs::rename(p / "from", p / "to"); // error: "to" is not empty fs::rename(p/"from", p/"to/subdir");// OK std::filesystem::remove_all(p);}
| renames a file (function)[edit] | |
(C++17)(C++17) | removes a file or empty directory removes a file or directory and all its contents, recursively (function)[edit] |