C++ Program to read text file and write in another text file in File Handling

By | 05.01.2017

Read text file and write in another text file


Write a C++ Program to read text file and write in another text file in File Handling. Here’s simple Program to read text file and write in another text file in File Handling in C++ Programming Language.


Below is the source code for C++ Program to read text file and write in another text file in File Handling which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to read text file and write in another text file in File Handling  */

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

int main()
{
    cout<<"Text File 1 is going to created........\n";
    ofstream fout("C:\\Users\\acer\\Documents\\file4.txt"); //create a file to write

    cout<<"\nText File 1 is opened for writing........\n";
    ifstream fin("C:\\Users\\acer\\Documents\\file4.txt");
    fout<<"Welcome to CodezClub......!!";

    cout<<"\nWriting to Text File 1 is successfully completed........\n";
    fout.close();                            //closing the file

    cout<<"\n\nText File 2 is going to create........\n";
    fout.open("C:\\Users\\acer\\Documents\\file5.txt"); //create file to write

    char ch;

    cout<<"\nText File 2 is opened for writing........\n";
    while(fin) //loop wiill run till end of file
    {
        fin>>ch;       //reading data from file
        fout<<ch;       //writing data to file
    }

    cout<<"\nWriting to Text File 2 from File 1 is successfully completed.\n";

    fin.close();
    fout.close();

    return 0;
}

OUTPUT : :


/*  C++ Program to read text file and write in another text file in File Handling  */

Text File 1 is going to created........

Text File 1 is opened for writing........

Writing to Text File 1 is successfully completed........


Text File 2 is going to create........

Text File 2 is opened for writing........

Writing to Text File 2 from File 1 is successfully completed.

Process returned 0

Above is the source code for C++ Program to read text file and write in another text file in File Handling which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments