Solved programming problem

How to read and write data from ".txt" file in C++.

Source code

#include <bits/stdc++.h>

using namespace std;

int main()

{

    ifstream file; //input file create

    file.open("ins.txt");

   ofstream file1; //output file create

   file1.open("out.txt");

    while(!file.eof())

    {

        string data;

        getline(file, data); //read full line from "file"

        file1<<data<<endl; //write data to the new "file1"

        cout<<data<<endl; //print in the console

    }

    file.close();

    file1.close();

  return 0;

}

Output

Create input file and write some message

Hello Everyone

Welcome to

Code to Haven

run the program & output file will be created

Join the discussion

Add your comment