Home > Backend Development > C++ > How to move files using C++?

How to move files using C++?

WBOY
Release: 2024-06-02 21:41:00
Original
509 people have browsed it

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 using C++?

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>
Copy after login

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");
Copy after login

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;
}
Copy after login

Note:

  • If the target file already exists, std::filesystem::rename will overwrite it.
  • If the source and target files are on different file systems, 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template