To copy the text/contents of one file to another file, we should know the basics of reading and writing a text file in C++.

To copy the file using C++, we read the contents of the source file and write it into the destination file.

Steps to copy one file to another in C++:

  1. Create objects of ifstream and ofstream classes.
  2. Check if they are connected to their respective files. If so, go ahead otherwise check the filenames twice.
    1. Read the contents of the source file using the getline() method and write the same to the destination using the << operator ( i.e. copy each line from ifstream object to ofstream object).
  3. Close files after the copy using the close() method.
  4. End the program.

Note: ifstream and ofstream classes are present in the <fstream> library.

Here is the C++ Program that implements the above steps to copy a .txt file:

#include <iostream>
#include <fstream>
using namespace std;
 
int main()
{
    string line;
    //For writing text file
    //Creating ofstream & ifstream class object
    ifstream ini_file {"original.txt"};
    ofstream out_file {"copy.txt"};
 
    if(ini_file && out_file){
 
        while(getline(ini_file,line)){
            out_file << line << "\n";
        }
 
        cout << "Copy Finished \n";
 
    } else {
        //Something went wrong
        printf("Cannot read File");
    }
 
    //Closing file
    ini_file.close();
    out_file.close();
 
    return 0;
}

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 example, we have assumed that both the original file and the copy file are in the same directory where the code file of this program is.

The above program runs unless the whole contents of the original file get copied to another file. If you have any doubts regarding this program then comment below.

This Post Has 2 Comments

  1. yashwant sahu
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        ifstream fs;
    	ofstream ft;
    	char ch, fname1[20], fname2[20];
    	cout<>fname1;
    	fs.open(fname1);
    	if(!fs)
    	{
    		cout<<"Error in opening source file..!!";
    		exit(1);
    	}
    	cout<>fname2;
    	ft.open(fname2);
    	if(!ft)
    	{
    		cout<<"Error in opening target file..!!";
    		fs.close();
    		exit(2);
    	}
    	while(fs.eof()==0)
    	{
    		ch=fs.get();
    		if(!fs.eof())
    		ft<<ch;
    	}
    	cout<<"File copied successfully..!!";
    	fs.close();
    	ft.close();
    
        return 0;
    }
  2. programmer LOL
    #include <iostream>
    #include <iostream>
    using namespace std;
    void file_checker(fstream *f)
    {
        string temp;
        getline(*(f), temp);
        if (temp == "")
        {
            cout << "Nothing to Copy";
            exit(1);
        }
    }
    int main()
    {
        fstream f1, f2;
        string s1;
        f1.open("file1.txt");
        f2.open("file2.txt");
    
        file_checker(&f1);
    
        while (!f1.eof())
        {
            getline(f1, s1);
            f2 << s1 << '\n';
        }
        cout << "Copied !";
    
        f1.close();
        f2.close();
        return 0;
    }

Leave a Reply