Copy File Contents Using C++

This exercise involves writing a C++ program to copy the entire content of one file into another. By completing this task, you will practice using file streams in C++, specifically `ifstream` for reading and `ofstream` for writing. You'll learn how to handle file input/output operations, which are crucial for many real-world applications involving data processing or backup systems. The program will read the source file line by line and write each line into the destination file, effectively duplicating the content.

Group

File Management in C++

Objective

1. Ask the user to enter the names of the source and destination files.
2. Open the source file using `ifstream` for reading.
3. Open the destination file using `ofstream` for writing.
4. Read each line from the source file and write it into the destination file.
5. Close both files and confirm that the content has been copied successfully.

Develop a program that copies the contents of one file to another.

Example C++ Exercise

 Copy C++ Code
#include <iostream>       // Include for input and output stream
#include <fstream>        // Include for file handling
#include <string>         // Include for using the string class

using namespace std;

int main() {
    string sourceFile, destinationFile;   // Variables to store filenames

    // Ask user for source and destination file names
    cout << "Enter the source file name: ";
    cin >> sourceFile;
    cout << "Enter the destination file name: ";
    cin >> destinationFile;

    ifstream inFile(sourceFile);          // Open the source file for reading
    ofstream outFile(destinationFile);    // Open the destination file for writing

    // Check if the source file opened successfully
    if (!inFile) {
        cout << "Error: Could not open the source file." << endl;
        return 1;   // Exit with error code
    }

    // Check if the destination file opened successfully
    if (!outFile) {
        cout << "Error: Could not open/create the destination file." << endl;
        return 1;   // Exit with error code
    }

    string line;    // Variable to hold each line read from the source file

    // Read and copy lines from source to destination
    while (getline(inFile, line)) {
        outFile << line << endl;    // Write the line to the destination file
    }

    // Close both files
    inFile.close();
    outFile.close();

    // Confirm successful copy
    cout << "File copied successfully from " << sourceFile << " to " << destinationFile << "." << endl;

    return 0;   // End of program
}

 Output

Enter the source file name: original.txt
Enter the destination file name: copy.txt
File copied successfully from original.txt to copy.txt.

Share this C++ Exercise


More C++ Programming Exercises of File Management in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Find and Replace a Word in a File Using C++

    In this exercise, you will create a C++ program that searches for a specific word in a text file and replaces it with another word. This task will help you strengthen your understa...

  • Save and Load a List of Numbers from a File in C++

    In this exercise, you will create a C++ program that allows the user to input a list of numbers, save them to a text file, and then read and display those numbers from the file. Th...

  • Count the Number of Lines in a Text File Using C++

    This exercise guides you through the process of writing a C++ program that reads a text file and counts the number of lines it contains. This is a fundamental operation when analyz...

  • Sort the Lines of a Text File Using C++

    This exercise involves creating a C++ program that reads all lines from a text file, sorts them in alphabetical order, and then displays the sorted lines on the screen. Sorting fil...

  • Delete a Specific File Using C++

    In this exercise, you will write a C++ program that deletes a specific file from the system using file handling operations. Managing files programmatically is an essential skill in...

  • ATM Transaction Logger in C++

    In this exercise, you will develop a simple ATM simulator in C++ that logs every transaction to a file. Logging is a vital component in real-world applications, especially in banki...