Replacing Number within a text File

Hey everyone.

I have a text file (Actually a .BFR file that I just renamed to .txt) that I need to update values in. I already created my C++ program that will open that file, search the file for whatever value I want to replace. And what lines those values occur on.

However, I can't seem to figure out how to actually replace those numbers with another one.
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
27
28
29
30
31
32
33
34
35

#include <cstdlib> 
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>

using namespace std;

int main()
{   
    ifstream in_stream;           
    string filein, search, str, replace; 
    int lines = 0, 
    char ch;
      
    cout << "Filename:\n";  
    cin >> filein;                            
    in_stream.open (filein.c_str(), ios::in | ios::binary); 
    
    
    //FIND
    cout << "Number to find replace: " <<endl; 
    cin >> search;

    while (!in_stream.eof())  
    {getline(in_stream, str); 
     lines++;                 
     if ((str.find(search, 0)) != string::npos) 
        {
        cout << "found at line " << lines << endl; 
        }
    }
    
    in_stream.seekg (0, ios::beg);


So this finds the numbers I'm looking for, then spits out the lines that they are on. This works as I confirmed with Notepad++.
Last edited on
That talks about finding and replacing a line though. My numbers are within a line. How would I find the position of them within the line to replace them.

For example, my line is something like this:

1
2
#blahblah  "A|B|32190|32190||BOOGIE||HAHA|M||||||NOPE|||||||||||
#DSFSD "O|1|32190|SDFLSDHJKF|SFSDF|SD|||SDFSD 


So basically I have about 500 numbers like that in sequence. And I want this program to search for 32190, increment it by 300, replace the 32190 with 32490.

Make sense?
You can read the whole line and then use string manipulation to replace whatever you want. I think reading and writing a single text line from a file is nothing for now-a-days' computers, right?
I'm still super confused on how to replace that number, and then write to the same file? Is that possible? Or do I have to delete that file, and then write a new one with the same name?
Can someone point me in the right direction?

Thanks.
Topic archived. No new replies allowed.