Search and replace

How would I be able to search for a number and change it in a text file?
I looked through many sites and this forum and have not yet found something useful.
For example:

file content:

12 dogs bark at the moon at 9
Lets say some bad weather happened so the dog delays its barking by 2 hours.
So now the text file should say

12 dogs bark at the moon at 11
Last edited on
Read the file into memory.

Change the data in memory.

Overwrite the file with the changes.



Reading the file as a collection of strings:

1
2
3
deque <string> data;
string s;
while (getline( f, s )) data.push_back( s );


Reading the file as a single string:

1
2
3
ostringstream ss;
ss << f.rdbuf();
string data = ss.str();

Hope this helps.
thanks!, I managed to do it some way but I can't figure out why it won't output to texfile but even then how I would I be able to replace the line with the intended output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
if (find(ids.begin(), ids.end(), newid) != ids.end())//Since the original line was stored in a vector i was able to use this to see if the input ID matches the file
{
   fstream edit;//opens file
   edit.open("books.txt");
   if (!edit)
      
    {cout<<"Error opening file"<<endl;
}
   
   cout<<"This book already exists"<<endl;
   unsigned int curLine = 0;
   string line;
   string search;
   cout<<"Enter ID again"<<endl;
   cin>>search;
//Gets line number which corresponds to the vector cell and i was able to update the copies of the books of the line
   while(getline(edit, line)) { // I changed this, see below
    curLine++;
    if (line.find(search) != string::npos) {
        cout << "found: " << search << "line: " << curLine << endl;
        copy[curLine-1]=copy[curLine-1]+1;
        cout<<copy[curLine-1];
        edit.clear();
        edit<<ids[curLine-1]<<titles[curLine-1]<<authors[curLine-1]<<cat[curLine-1]
        <<copy[curLine-1];//This is the troublesome part won't output to texfile no changes were made to the file and I want this to replace the line
    }


int n1, n2;
string str;

get n1;
get str;
get n2;

n2 += 2;

write n1+" " + str + " " + n2;
Topic archived. No new replies allowed.