Answer to Question #180727 in C++ for Prathamjain

Question #180727

Write a program in C++ to open and read the contents of th . ”  of the current e 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 After update Text1.txt: I am enjoying learning OOPS concepts Now learnt C++; Java is my next target


1
Expert's answer
2021-04-12T15:23:16-0400
#include <fstream>
#include <iostream>
using namespace std;

int main () {
   string f,srg;
   //file stream class variable for reading
   ifstream readfile;

   //file stream class variable for writing
   ofstream writefile;
   // open a file in read mode.
   readfile.open("Text1.txt");

   cout << "Reading from the file" << endl;
   //way of reading
   while ( getline (readfile,srg) )
    {
      cout << srg <<endl;
    }
   // closing the file.
   readfile.close();
   //ios::app is for appending text in file
   // open file again for updating content.
   writefile.open("Text1.txt",ios::app);
   cout << "Writing to the file" << endl;

   //text for updating
   f ="Now learnt C++; java is my next Target";
   // write updated text into the file.
   writefile << f << endl;
   // closing the file.
   writefile.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