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