Program to read from a text file and then write in another text file

Posted in

Program to read from a text file and then write in another text file
vinaykhatri

Vinay Khatri
Last updated on April 24, 2024

    Every High-Level Programming language supports file handling, which helps us to write, read and append data into text and binary files.

    In this Programming tutorial, we will learn how to write a script in C++ and Python to read data from one file and write it into another. First, we will write data into out file1.txt file, then read that data and write it into another file2.txt file.

    C++ Program to read from a text file and then write in another text file

    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      char ch; 
      fstream file1,file2;
      
      //open first file
      file1.open ("file1.txt",ios::out);
      cout<<"Writing data into file1.txt file\n";
      
      //writing data into first file
      file1<<"C++ Hello World Welcome to Techgeekbuzz";
      file1.close();
      //close first file
      
      //open first file to read
      file1.open("file1.txt", ios::in);
      //open second file to write
      file2.open("file2.txt", ios::out);
      while(file1)
      {
      	//read data from file 1
      	file1.get(ch);
      	cout<<ch;
      	//write data to file 2
      	file2<<ch;
      }
      cout<<"\nFile1 data has been written into file2";
      //close file 2
      file2.close();
      
      //close file 2
      file1.close();
      
      return 0;
    }

    Output

    Writing data into file1.txt file
    C++ Hello World Welcome to Techgeekbuzzz
    File1 data has been written into file2

    Python Program to read from a text file and then write in another text file

    #open first file in write mode
    with open("file1.txt", 'w') as file1:
        print("Writing data into file1.txt file")
        #write some data into first file
        file1.write("Python! Hello World Welcome to Techgeekbuzz")
        
    
    #open first file in read mode
    with open("file1.txt", 'r') as file1:
        #read first file data 
        data = file1.read()
    
        #open second file in write mode
        with open("file2.txt", 'w') as file2:
            #write first file data into second file
            file2.write(data)
            print("File1 data has been written into file2")

    Output

    Writing data into file1.txt file
    File1 data has been written into file2

    After executing the program, the data of file1.txt will be written in another file file2.txt .

    Wrapping UP!

    In this programming tutorial, we learned how to read data from a text file and how to write it into another text file. But here, instead of directly reading from a file, we first create a file, write some data in that, then read that data and write it into another file. To understand the above program, you must have a basic knowledge of file handling and how to perform file handling in C++ and Python.

    To know more about Python file handling, click here , and for C++ file handling, click here

    People are also reading:

    Leave a Comment on this Post

    0 Comments