File Handing Read and Write Data and add new data
#include<iostream>;
#include<fstream>
#include<string>
using namespace std;
int main() {
string olddata;
//ios::in | ios::out | ios::trunc
fstream file_obj("sabir.xml", ios::in | ios::out | ios::trunc);
string line;
if (file_obj.is_open()) {
cout << "File is Open";
file_obj << "Here a detailed overview and example of file handling in C++:";
//====Read data========
file_obj.seekg(0);
getline(file_obj, line);
cout << "Data=" << line;
//======Write data====
file_obj.seekp(5);
file_obj << "new data ";
//====Read data========
file_obj.seekg(0);
getline(file_obj, line);
cout << "Data=" << line;
////====Read data=======
//file_obj.seekg(5);
//getline(file_obj, olddata);
file_obj.close();
}
return 0;
}
Comments
Post a Comment