C++ Program to Read Content From One File and Write it Into Another File Last Updated : 04 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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 file stream object and open file.txt in it.Create an output file stream object and open file2.txt in it.Read each line from the file and write it in file2. Below is the C++ program to read contents from one file and write it to another file: C++ // C++ program to read contents from // one file and write it to another file #include<bits/stdc++.h> using namespace std; // Driver code int main() { // Input file stream object to // read from file.txt ifstream in("file1.txt"); // Output file stream object to // write to file2.txt ofstream f("file2.txt"); // Reading file.txt completely using // END OF FILE eof() method while(!in.eof()) { // string to extract line from // file.txt string text; // extracting line from file.txt getline(in, text); // Writing the extracted line in // file2.txt f << text << endl; } return 0; } Output: file1.txt GeeksforGeeks is a Computer Science portal for geeks. file2.txt GeeksforGeeks is a Computer Science portal for geeks. Comment More info I ishankhandelwals Follow Improve Article Tags : C++ Programs C++ C++ File Programs Explore C++ BasicsIntroduction to C++3 min readData Types in C++6 min readVariables in C++4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readLoops in C++7 min readFunctions in C++8 min readArrays in C++8 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readOOP in C++Object Oriented Programming in C++8 min readInheritance in C++6 min readPolymorphism in C++5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Standard Template Library (STL) in C++3 min readContainers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library3 min readPractice & ProblemsC++ Interview Questions and Answers1 min readC++ Programming Examples4 min read Like