I want to replace a string aka word "maaza" with word "fanta" . I actually tried but the replacement goes wrong and replace other string with it so here is my code and i am attaching a ss of file
#include<iostream>
#include<fstream>
using namespace std;
int main(){
fstream file;
string filename="stockk.txt";
file.open(filename.c_str());
string s1;
string s2="fanta";
int pos;
int flag=0;
while(file>>s1){
if(s1=="maaza"){
flag++;
// pos=file.tellg();
s1=s2;
file<<s1;
break;
}
}
if(flag==0){
cout<<"Go ahead";
}
if(flag==1){
cout<<"data already recorded at address "<<pos;
// file.seekg(pos,ios::beg);
// file.seekp(74,ios::beg);
// file<<"";
}
return 0;
}
and the file before replacement was like this
Name : rasana
Cost : 300
Price : 400
Quantity : 5
Name : maaza
Cost : 300
Price : 400
Quantity : 1
After replacement code it was like this
Name : rasana
Cost : 300
Price : 400
Quantity : 5
Name : maaza
Costfanta0
Price : 400
Quantity : 1
so you can see the mistake please help.