Home > Article > Backend Development > How to move files using C++?
In C, you can use the std::filesystem::rename function to move files. This function requires two parameters: the source file path and the target file path.
How to move files in C
Moving files is an operation of transferring files from one location to another. without creating a copy. In C, file movement can be easily achieved using the std::filesystem
library.
Header file inclusion
Before using std::filesystem
, you need to include the header file:
#include <filesystem>
File movement
To move files, you can use the std::filesystem::rename
function. This function requires two parameters: source file path and target file path.
std::filesystem::rename("source_file.txt", "target_file.txt");
Practical case
The following is a practical case of how to use std::filesystem::rename
to move files:
#include <filesystem> int main() { std::filesystem::rename("old_file.txt", "new_file.txt"); std::cout << "File moved successfully." << std::endl; return 0; }
Note:
std::filesystem::rename
will overwrite it. std::filesystem::rename
will fail. The above is the detailed content of How to move files using C++?. For more information, please follow other related articles on the PHP Chinese website!