title: Overwrite an existing text file c++
body: <p>This is how my Save As works - it is copying the current file's lines until it reaches the first figure and then I use my print methods to print the figure's info and then close the  tag.</p>
  
  <pre><code>std::ofstream newFile(filePath1_fixed, std::ios::app);
  std::fstream openedFile(filePath);
  std::string line1;
  while (std::getline(openedFile, line1)) {
      if (line1.find("&lt;rect") != std::string::npos
          || line1.find("&lt;circle") != std::string::npos
          || line1.find("&lt;line") != std::string::npos)
          break;
      newFile &lt;&lt; line1 &lt;&lt; std::endl;
  }
  figc.printToFile(newFile);
  newFile &lt;&lt; "&lt;/svg&gt;\n";
  </code></pre>
  
  <p>My question is how to save the changes to the current file? I tried something like this:</p>
  
  <pre><code>std::ifstream openedFile(filePath);
  std::ofstream newFile(filePath, std::ios::app);
  std::string line1;
  std::string info_beg[100];
  int t = 0;
  while (std::getline(openedFile, line1)) {
      std::cout &lt;&lt; "HELLYEAH";
      if (line1.find("&lt;rect") != std::string::npos
          || line1.find("&lt;circle") != std::string::npos
          || line1.find("&lt;line") != std::string::npos)
          break;
      info_beg[t++] = line1;
  }
  for (int i = 0; i &lt; t; i++)
      newFile &lt;&lt; info_beg[i] &lt;&lt; std::endl;
  figc.printToFile(newFile);
  newFile &lt;&lt; "&lt;/svg&gt;\n";
  </code></pre>
  
  <p>This is the nearest I've gone. I get this:</p>
  
  <pre><code>  &lt;?xml version="1.0" standalone="no"?&gt;
  &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"&gt;
  &lt;svg width="12cm" height="4cm" viewBox="0 0 1200 400"
       xmlns="http://www.w3.org/2000/svg" version="1.1"&gt;
    &lt;desc&gt;Example rect01 - rectangle with sharp corners&lt;/desc&gt;
  
    &lt;!-- Show outline of canvas using 'rect' element --&gt;
    &lt;rect x="1" y="1" width="1198" height="398"
          fill="none" stroke="blue" stroke-width="2" /&gt;
  
    &lt;line x1="20" y1="100" x2="100" y2="20"
          stroke="red" stroke-width="2" /&gt;
  
    &lt;rect x="20" y="30" width="40" height="50"
          fill="red" stroke="red" stroke-width="1" /&gt;
  
    &lt;rect x="10" y="20" width="30" height="40"
          fill="red" stroke="blue" stroke-width="1" /&gt;
  
    &lt;line x1="100" y1="200" x2="300" y2="400"
          stroke="red" stroke-width="2" /&gt;
  
    &lt;circle cx="10" cy="20" r="30"
          fill="red" stroke="blue" stroke-width="2" /&gt;
  
  &lt;/svg&gt;
  &lt;?xml version="1.0" standalone="no"?&gt;
  &lt;!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"&gt;
  &lt;svg width="12cm" height="4cm" viewBox="0 0 1200 400"
       xmlns="http://www.w3.org/2000/svg" version="1.1"&gt;
    &lt;desc&gt;Example rect01 - rectangle with sharp corners&lt;/desc&gt;
  
    &lt;!-- Show outline of canvas using 'rect' element --&gt;
    &lt;rect x="1" y="1" width="1198" height="398"
          fill="none" stroke="blue" stroke-width="2" /&gt;
  
    &lt;line x1="20" y1="100" x2="100" y2="20"
          stroke="red" stroke-width="2" /&gt;
  
    &lt;rect x="20" y="30" width="40" height="50"
          fill="red" stroke="red" stroke-width="1" /&gt;
  
    &lt;rect x="10" y="20" width="30" height="40"
          fill="red" stroke="blue" stroke-width="1" /&gt;
  
    &lt;line x1="100" y1="200" x2="300" y2="400"
          stroke="red" stroke-width="2" /&gt;
  
    &lt;circle cx="10" cy="20" r="30"
          fill="red" stroke="blue" stroke-width="2" /&gt;
  
    &lt;rect x="10" y="20" width="30" height="40"
          fill="red" stroke="blue" stroke-width="2" /&gt;
  
  &lt;/svg&gt;
  </code></pre>
  
  <p>So my actual question is how to delete the first  or overwrite it or I need a different approach.</p>