Answer to Question #180932 in C++ for bvgvbhb

Question #180932

Write a program in C++ to open and read the contents of the Text1.txt using the file stream class. Close the file and again open to update the contents of given file Text1.txt. Text1.txt : I am enjoying learning OOPS concepts


1
Expert's answer
2021-04-13T06:29:56-0400
#include <iostream>
#include <fstream>


using namespace std;


int main()
{
    fstream fs;
    string content;
    fs.open("Text1.txt", fstream::in | fstream::out | fstream::app);
    cout << "File contents:" << endl;
    if(fs.is_open())
    {
        while(!fs.eof())
        {
            content = "";
            fs >> content;
            cout << content << endl;
        }
    }
    else
        cout << "File haven't been opened:(" << endl;
    fs.close();


    fs.open("Text1.txt", fstream::in | fstream::out | fstream::app);
    if(fs.is_open())
    {


        content = "I am enjoying learning OOPS concepts";
        fs <<  content << "\n";
        cout << "File contents:" << endl;
        fs.seekg(0);
        while(!fs.eof())
        {
            content = "";
            fs >> content;
            cout << content << endl;
        }
    }
    else
        cout << "File haven't been opened:(" << endl;
    fs.close();


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS