title: Renaming a file to name of file that already exists
body: <p>i am renaming file for example <code>file1.txt</code> to <code>"newFile.txt"</code> but when the code runs for the next time. the file <code>newFile.txt</code> already exists so the newly file created whos name is <code>"file1.txt"</code> doesn't renamed to <code>"newFile.txt"</code>
  <br>
  what i want is if the <code>"newFile.txt"</code> already exists renaming <code>"file1.txt"</code> should overwrite the <code>"file1.txt"</code> is it possible ??</p>
  
  <p>Here goes my code</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main() {
      char data[100];
  
      fstream outfile;
      outfile.open("afile.dat" , ios::out );
  
      cout &lt;&lt; "Writing to the file" &lt;&lt; endl;
      cout &lt;&lt; "Enter your name: ";
      cin.getline(data, 100);
  
      outfile &lt;&lt; data &lt;&lt; endl;
  
      cout &lt;&lt; "Enter your age: ";
      cin &gt;&gt; data;
      cin.ignore();
  
      // again write inputted data into the file.
      outfile &lt;&lt; data &lt;&lt; endl;
  
      cout &lt;&lt; "Reading from the file" &lt;&lt; endl;
  
      outfile &gt;&gt; data;
      cout &lt;&lt; data &lt;&lt; endl;
  
      outfile &gt;&gt; data;
      cout &lt;&lt; data &lt;&lt; endl;
      outfile.close();
  
     // this is how i am renaming
      std::rename("afile.dat" , "file2.txt");
  
      return 0;
  }
  </code></pre>