In C++, you can use the fstream library to copy one file to another. Here is an
example of how to do this:
Copy code
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Open the files
ifstream src("original.txt", ios::binary);
ofstream dest("copy.txt", ios::binary);
// Check if the files were successfully opened
if (!src.is_open()) {
cout << "Error opening original file." << endl;
return 1;
}
if (!dest.is_open()) {
cout << "Error opening copy file." << endl;
return 1;
}
// Copy the contents of the original file to the copy file
dest << src.rdbuf();
// Close the files
src.close();
dest.close();
cout << "File copied successfully." << endl;
return 0;
}
This code uses ifstream to open the file "original.txt" for reading and ofstream
to open the file "copy.txt" for writing. The src.is_open() and dest.is_open()
checks are used to ensure that the files were opened successfully.
The dest << src.rdbuf(); copies the contents of the "original.txt" to "copy.txt"
file.
It's important to note that the use of ios::binary flag is important when you are
copying binary files such as images, audio and video files, it ensures that the
data is copied as it is, and not interpreted as text.
Also, it's important to note that if the destination file already exists, it will
be overwritten by the contents of the source file.
You can use the C++ standard library's filesystem library introduced in C++17,
to copy one file to another, by using std::filesystem::copy_file(from,to) function,
where 'from' is the source file and 'to' is the destination file.
To embed this project on your website, copy the following code and paste it into your website's HTML: