Copy File in C++

Copying a File in C++.

IntermediateFile Handling ProgramsExample 6 of 6
copy-file.cpp
1#include <iostream>
2#include <fstream>
3#include <filesystem>
4using namespace std;
5using namespace filesystem;
6
7int main() {
8 string sourceFile = "data.txt";
9 string destFile = "data_copy.txt";
10
11 // Method 1: Using filesystem (C++17) - Simple
12 try {
13 copy_file(sourceFile, destFile);
14 cout << "File copied successfully using filesystem!" << endl;
15 } catch (const filesystem_error& e) {
16 cout << "Error: " << e.what() << endl;
17 }
18
19 // Method 2: Manual copy - Read and write
20 ifstream source(sourceFile, ios::binary);
21 ofstream dest(destFile + "_manual", ios::binary);
22
23 if (source.is_open() && dest.is_open()) {
24 dest << source.rdbuf(); // Copy entire file buffer
25
26 source.close();
27 dest.close();
28
29 cout << "File copied manually!" << endl;
30 } else {
31 cout << "Error opening files for manual copy." << endl;
32 }
33
34 // Verify copy
35 if (exists(destFile)) {
36 cout << "\nVerification:" << endl;
37 cout << "Source size: " << file_size(sourceFile) << " bytes" << endl;
38 cout << "Copy size: " << file_size(destFile) << " bytes" << endl;
39
40 if (file_size(sourceFile) == file_size(destFile)) {
41 cout << "File sizes match - Copy verified!" << endl;
42 }
43 }
44
45 return 0;
46}

Output

File copied successfully using filesystem!
File copied manually!

Verification:
Source size: 156 bytes
Copy size: 156 bytes
File sizes match - Copy verified!

What's going on

The program demonstrates file copying:

Method 1: Using filesystem::copy_file() (C++17)
Method 2: Manual copy using read/write
Using rdbuf() for efficient buffer copying
Verifying copy by comparing file sizes

File copying enables backup, duplication, and file management.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <fstream>
Provides file stream classes (ifstream, ofstream).
3.#include <filesystem>
Provides filesystem operations (C++17).

Understanding File Copying

Copy Concept

:

Duplicate source file to destination
Preserve all content exactly
Create new file with same data
Essential for backup and management

Copy Methods

:

filesystem::copy_file(): simple, C++17
Manual read/write: more control
rdbuf(): efficient buffer copy
Binary mode for exact copy

Method 1: Using filesystem::copy_file()

Simple Copy

:

copy_file(sourceFile, destFile);

How it works

:

One-line file copy
Handles all details automatically
Exception on error
C++17 required

Error Handling

:

try {

copy_file(sourceFile, destFile);

} catch (const filesystem_error& e) {

}

1cout << "Error: " << e.what() << endl;

Method 2: Manual Copy

Using rdbuf()

:

ifstream source(sourceFile, ios::binary);

ofstream dest(destFile, ios::binary);

dest << source.rdbuf();

How it works

:

rdbuf(): copies entire file buffer
Efficient one-operation copy
Works with binary mode
Preserves exact content

Binary Mode

:

ios::binary: exact byte copy
No text conversion
Preserves all data
Recommended for exact copy

Verifying Copy

Comparing File Sizes

:

if (file_size(sourceFile) == file_size(destFile)) {

}

1cout << "Copy verified!" << endl;

How it works

:

Compare source and destination sizes
Same size indicates successful copy
Can also compare contents
Verifies copy operation

When to Use Each Method

filesystem::copy_file()

:

C++17 available
Simple one-line copy
Automatic error handling
Recommended for new code

Manual Copy

:

More control needed
Older C++ standards
Custom copy logic
Binary file handling

Important Considerations

File Modes

:

Use ios::binary for exact copy
Preserves all data types
No text conversion issues
Essential for binary files

Error Handling

:

Check if source exists
Handle write permissions
Verify destination writable
Provide user feedback

File Overwriting

:

Destination may be overwritten
Check if destination exists
Ask user confirmation if needed
Handle overwrite carefully

return 0;

This ends the program successfully.