xyz

/*
Write a C++ program that creates an output file, writes information to it, closes the file
and open it again as an input file and read the information from the file.
*/

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

int main() {

    // creates an output file.
    ofstream outFile(“example.txt”);

    if (!outFile.is_open()) {
        cerr<<“Error: opening output file”;
        return -1;
    }
    // writes information to it.
    outFile<<“Hello! This is output file.”<<endl;
    outFile<<“This is another File.”<<endl;
    outFile<<“name is ANI”<<endl;

    //Closeing the output Filea
    outFile.close();

    // open it again as an input file

    ifstream inFile(“example.txt”);

    if (!inFile.is_open()) {
        cerr<<“Error: opening output file”;
        return -1;
    }

    string line;
    while (getline(inFile, line))
    {
        cout<<line<<endl;
    }

    inFile.close();
   

    return 0;
}
Scroll to Top