title: "Updating" a line in a file by overwriting it
body: <p>I'm trying to write a function that when called, will search my text file for a match to the parameter, then "update" it by overwriting the file.</p>
  
  <p>here is my current function:</p>
  
  <p>EDIT: Here is the new updated code, still not working :/</p>
  
  <p>Here is the line it sends to UpdateGem: (Example sTempTxt: "AB:5.55")</p>
  
  <pre><code>ostringstream stream; 
  stream &lt;&lt; std::setprecision(3)  &lt;&lt; fEindgem;
  string sTempTxt = sVak + ":" + stream.str() + "\n";
  UpdateGem(sTempTxt);
  
  void UpdateGem(string text)
  {
       ifstream f;
       f.open("GEM.txt");
       string sGEMS[100];
       string temp[3];
       splitstring(text, ":", temp[0], temp[1]);
       bool OverWrite;
       int count = 0;
       string Delete, line;
  
       while(true)
       {
           getline(f, sGEMS[count], '\n');
           if(f.eof()) break;
           splitstring(sGEMS[count], ":", temp[1], temp[2]);
           if (temp[0] == temp[1])
           {
                OverWrite = 1;
                Delete = sGEMS[count];
           }
           count++;
       }
  
  // Don't set count to 0, since we need it
  // count = 0;
     ofstream f2;
     f2.open("GEM2.txt"/*,std::ios_base::app*/);
     if (OverWrite) 
     {
        f.seekg(0, std::ios::beg);
        for (int i = 0; i &lt; count; ++i) 
        {
           if (sGEMS[i] != Delete) f2 &lt;&lt; sGEMS[i];
        }
     }
       f.close();
       f2.close();
       remove("GEM.txt");
       rename("GEM2.txt", "GEM.txt");
  
       ofstream file;
       file.open("GEM.txt",std::ios_base::app);
       file &lt;&lt; text;
       file.close();     
  
  
  }
  </code></pre>
  
  <p>The line it has to replace takes the form of NAME:NUMBER, in which the number can be different, so I am using a splitstring function to compare the name to the name of the line found, then completely erasing that line and "updating" it by re-adding it later. However, my current code only writes the updated line to the file, not the old ones...</p>