0

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.

Mahir Jain
  • 37
  • 7
  • Have you ever used the debugger? – Karen Baghdasaryan Aug 27 '21 at 16:10
  • i never used a debugger i am new to this so thats why. so is there any way to solve this ? – Mahir Jain Aug 27 '21 at 16:14
  • 2
    A good place to start is to read the whole file in memory. Replace everything you want and then to write the file only once. It's also described here : https://stackoverflow.com/questions/37931691/replace-a-word-in-text-file-using-c. When writing a file, make sure you write to a temp file first then swap (rename the files) and delete the old one. You will always have a backup file that way (https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom) – Pepijn Kramer Aug 27 '21 at 16:15
  • @MahirJain Then I'd strongly recommend to learn to use debugger. It's tool that facilitates the process of finding bugs in the code. You can read about the debugger [here](https://learn.microsoft.com/en-us/visualstudio/debugger/what-is-debugging?view=vs-2019). In your code it's obvious that debugging once would help you to immediately find the cause of the problem. – Karen Baghdasaryan Aug 27 '21 at 16:19
  • okay . can i get my desired output the way i coded after tweaking the code – Mahir Jain Aug 27 '21 at 16:19
  • ohh thank i will learn – Mahir Jain Aug 27 '21 at 16:22
  • `pos` is not initialized, so the line `cout<<"data already recorded at address "< – yano Aug 27 '21 at 16:24
  • 2
    it's better to load whole file into memory, do replacements, then write back to file – Errorist Aug 27 '21 at 16:31
  • ohk i think i should use this way of replacement – Mahir Jain Aug 27 '21 at 16:34

1 Answers1

0

As I can see, in your code you tried to remember the position of file, but you did it after receiving string from stream. In your file it looks like "maaza\nCost", thats why it writes right after it. So i suggest it must look smth like this:

fstream file("stockk.txt");

int pos;
string s1, s2 = "fanta";

// While file not over
while (!file.eof())
{
    // getting stream cursor pos
    pos = file.tellg();
    file >> s1;

    if (s1 == "maaza")
    {
        file.seekg(pos); // Returning to pos we remembered
        file << ' ' << s2; // ' ' is because you have spaces between ':' and "maaza"
        break;
    }
}

Btw you should know, you cant replace any word in text without loading whole file in your memory. This method works only for word with same size

ReNeReS
  • 31