title: Search and replace string in txt file in c++
body: <p>I want to find a string in a file and replace it with user input.<br>
  Here is my rough code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream.h&gt;
  #include &lt;string.h&gt;
  
  int main(){
      istream readFile("test.txt");
  
      string readout,
           search,
           replace;
  
      while(getline(readFile,readout)){
          if(readout == search){
              // How do I replace `readout` with `replace`?
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong><br>
  Here is the code that solved my problem</p>
  
  <p>test.txt:</p>
  
  <pre><code>id_1
  arfan
  haider
  
  id_2
  saleem
  haider
  
  id_3
  someone
  otherone
  </code></pre>
  
  <p>C++ Code:  </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namesapce std;
  
  int main(){
      istream readFile("test.txt");
      string readout,
             search,
             firstname,
             lastname;
  
      cout &lt;&lt; "Enter the id which you want to modify";
      cin &gt;&gt; search;
  
      while(getline(readFile,readout)){
          if(readout == search){
              /*
                  id remains the same
                  But the First name and Last name are replaced with
                  the user `firstname` and `lastname` input
              */
              cout &lt;&lt; "Enter new First name";
              cin &gt;&gt; firstname;
  
              cout &lt;&lt; "Enter Last name";
              cin &gt;&gt; lastname;  
          }
      }
  }  
  </code></pre>
  
  <p>Suppose:<br>
  A user searches for id <code>id_2</code>. After that user enter First name and Last name <code>Shafiq</code> and <code>Ahmed</code>.
  After runing this code the <code>test.txt</code> File must modify the record like that:  </p>
  
  <pre><code>…
  
  id_2
  Shafiq
  Ahmad
  
  …
  </code></pre>
  
  <p>Only the <code>id_2</code> record changes, the remaining file will stay the same.  </p>