Click here to Skip to main content
14,733,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to find a word an replace it in the text file. Now, i can find the word successfully in the text file but i am unable to replace it. Can anyone please suggest me where i made the mistake. Thanks.

#include <iostream>
#include <fstream>

using namespace std;

int main(){
ifstream input;
ofstream output;"Book1.txt",ios::app);

int choice;
size_t pos;
size_t p;
string line;
string a;
string id;
cout<<"enter the name you want to search"<<endl;

string replace = "hello";
size_t len = a.length();"Book1.txt",ios::out);
pos = line.find(a);


cout<<"Name://IDnumber//Account-Balance"<<" "<<line<<endl;



Updated 6-Nov-19 3:33am
Richard MacCutchan 20-Nov-14 7:43am
You should have an output file, and every time you read a line, you also write it to the output file, whether it is changed or not.

1 solution

read the whole file in a string and use replace.

Check that all strings are replaced and than write the new string to the file
Surajit Das 20-Nov-14 5:23am
@KarstenK- I am sorry but i couldnt understand. Can you give me an example?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900