To copy the text/contents of one file to another file, we first need to understand how to:

Steps to Copy One File to Another

  1. Make ifstream and ofstream objects.
  2. Check if they are connected to their respective files. If yes then go to Step 3, else Step 5.
  3. Using while loop, getline() method and << operator, copy each line from ifstream object to ofstream object.
  4. Close files using close() method.
  5. End Program.

C++ Program

Output of Program

Original File: original.txt

C++ Program to copy one file to another

Copy program running.

output of copy file program

Copy File:  copy.txt

Copy one File to another in C++

 

In this program, we have assumed that the original file and copy file is in the same directory where the code file of this program is. This program will run unless the whole contents of the original file get copied to another file. If you have any doubts regarding this program then comment below.

Leave a Reply

Close Menu