2 different C++ program to copy the content of one file to another

How to copy the content of one file to another in C++:

In this post, we will learn how to copy the content of one file to another file in C++. We will create two files in the same folder of the C++ program. One file will hold text and the other file will be empty. Once you run the program, the text will be copied to the empty file.

Method 1: C++ program to copy the content of one file to another by using fgetc and fputc:

Before you start with the program, make sure to create two files in the same folder where you will create the C++ program file.

In this example, I have created two text files inputFile.txt and outputFile.txt in that folder. The example program file is placed in a file example.cpp in that same folder.

The content of the inputFile.txt are:

Hello World
Hello World !!
Hello World !!!

outputFile.txt is an empty file.

Copy and paste the below code to the example.cpp file:

#include <iostream>
using namespace std;

int main()
{
    char sourceFile[15] = "inputFile.txt";
    char destinationFile[15] = "outputFile.txt";

    FILE *fileSource = fopen(sourceFile, "r");
    FILE *fileDestination = fopen(destinationFile, "w");

    if (fileSource != NULL && fileDestination != NULL)
    {
        char c = fgetc(fileSource);

        while (c != EOF)
        {
            fputc(c, fileDestination);
            c = fgetc(fileSource);
        }
    }

    fclose(fileSource);
    fclose(fileDestination);

    cout << "Completed!!" << endl;
}

Here,

  • The character arrays sourceFile and destinationFile holds the path of the source and destination files.
  • We created two FILE pointers fileSource and fileDestination for the input and the output files.
  • If the FILE variables are not NULL, it uses a while loop to read the file contents from the source file and write these to the destination file. It uses fgetc and fputc methods to read and write the contents.
  • Once the content is copied to the destination file, it closes the files by using the fclose method.

If you run this program, you will find the content of inputFile.txt copied to outputFile.txt.

Method 2: C++ program to copy the content of one file to another by using fstream:

The fstream packages provides ways to create input and output file streams. We can use it to copy the content of one file to another. We have to create one ifstream and one ofstream object. By using a while loop, we can read the content with the getline() method and write it to the output file by using the ofstream object.

Below is the complete program:

#include <fstream>
#include <iostream>
using namespace std;

int main()
{
    char sourceFile[15] = "inputFile.txt";
    char destinationFile[15] = "outputFile.txt";
    string currentLine;

    ifstream fileSource{sourceFile};
    ofstream fileDestination{destinationFile};

    if (fileSource && fileDestination)
    {

        while (getline(fileSource, currentLine))
        {
            fileDestination << currentLine + "\n";
        }
    }

    fileSource.close();
    fileDestination.close();

    cout << "Completed!!" << endl;
}

It is almost similar to the previous example.

  • We created one ifstream and one ofstream object pointing to the source and the destination file.
  • The getline method reads the lines of the file one by one and writes it to the destination file.
  • The file streams are closed at the end of the program.

If you run this program, it will copy the content from the source to the destination file.

You might also like: