C++ Program to Copy One File into Another File Last Updated : 20 Jun, 2022 Comments Improve Suggest changes Like Article Like Report 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++: Create objects of ifstream and ofstream classes.Check if they are connected to their respective files. If so, go ahead otherwise check the filenames twice.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).Close files after the copy using the close() method.End the program. Note: ifstream and ofstream classes are present in the <fstream> library. Example: C++ // C++ Program to demonstrate // copying the content of a .txt file #include <fstream> #include <iostream> using namespace std; int main() { string line; // For writing text file // Creating ofstream & ifstream class object ifstream ini_file{ "original.txt" }; // This is the original file 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; } Output: Original File – original.txt: File: original.txt Copy program running:  Copy File – copy.txt: Copied file: copy.txt 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. Comment More infoAdvertise with us Next Article C++ Program to Copy One File into Another File V vaishnavideshmane Follow Improve Article Tags : C++ Programs C++ C++ File Programs Practice Tags : CPP Similar Reads C++ Program to Copy the Contents of One File Into Another File Here, we will see how to develop a C++ program to copy the contents of one file into another file. Given a text file, extract contents from it and copy the contents into another new file. After this, display the contents of the new file. Approach:Open the first file which contains data. For example, 2 min read C++ Program to Read Content From One File and Write it Into Another File Here, we will see how to read contents from one file and write it to another file using a C++ program. Let us consider two files file1.txt and file2.txt. We are going to read the content of file.txt and write it in file2.txt Contents of file1.txt: Welcome to GeeksForGeeks Approach: Create an input f 2 min read C++ program to append content of one text file to another Given source and destination text files. Append the content from the source file to the destination file and then display the content of the destination file.Example : Input : file.txt : "geeks", file2.txt : "geeks for" Output: file2.txt : "geeks for geeks" Method 1:Approach: Open file.txt in inputs 3 min read How to Copy One Array to Another in C++? In C++, arrays are a type of data structure that stores a fixed-size collection of elements of the same type in a contiguous memory location, and sometimes we need to copy the contents of one array to another. In this article, we will learn how to copy one array to another in C++. Example: Input: ar 2 min read C++ Program to Get the List of Files in a Directory Getting the list of files in a directory is one of the most common operations performed by the Filesystem of an OS. The file explorer application in most operating systems performs the same operation in the background. In this article, you will learn how to get the list of files in a directory using 4 min read Like