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
#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;
}
Comments
Leave a comment