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
After update
Text1.txt: I am enjoying learning OOPS concepts
Now learnt C++; Java is my next target.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream fio;
string line;
fio.open("Text1.txt", ios::in);
if(!fio) {
cout << "No Such File";
}
else {
while (getline(fio, line)) {
cout << line << endl;
}
fio.close();
fio.open("Text1.txt", ios::app);
fio << "\nNow learnt C++;Java is my next target.";
fio.close();
return 0;
}
}
Comments
Leave a comment