Advertisement
file-stream-object("filename", mode);
File Mode | Description |
---|---|
ios::in" | Searches for the file and opens it in read mode only(if the file is found). |
ios::out | Searches for the file and opens it in write mode. If the file is found, its content is overwritten. If file is not found, a new file is created. Allows you to write to the file. |
"ios::binary" | Searches for the file and opens the file(if the file is found) in a binary mode to perform binary input/output file operations. This mode when used with ios::binary, ios::in and ios::out modes, allows you to modify the content of a file |
Advertisement
Gender : Male
Age : 28
City : Tokyo
Weight : 78.6 Kg
//Modifying the data to a file using fstream class and modes ios::
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
//Creating an output stream to append new data to a file
fstream ofstream_ob;
//Opening a file named country1.txt to modify the old content
ofstream_ob.open("File1.txt", ios::in|ios::out|ios::binary);
char ch;
while(ofstream_ob)
{
ch = ofstream_ob.get();
if(ch =='e')
{
//Going one byte position back from the current put pointer position
ofstream_ob.seekp(-1, ios::cur);
ofstream_ob.put('X');
}
}
//Setting the EOF flag off, to allow the access of file again for reading/writing
ofstream_ob.clear();
cout<<"The modified content of the file : \n";
//Taking the get pointer at the beginning of the file
ofstream_ob.seekg(0, ios::beg);
//Reading and displaying the modified file
while(ofstream_ob)
{
ch = ofstream_ob.get();
cout<<ch;
}
ofstream_ob.close();
return 0;
}
The modified content of the file :
GXndXr : MalX
AgX : 28
City : Tokyo
WXight : 78.6 Kg
ofstream_ob.seekp(-1, ios::cur);;
ofstream_ob.seekg(0, ios::beg);
ofstream_ob.clear();
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement