title: How to clear a file in append mode in C++
body: <p>I've a file on which I require multiple operations. Sometimes I just want to append data at the end of the file, sometimes I just want to read from the file, and sometimes, I want to erase all the data and write form the beginning of the file. And then, I again need to append data at the end of file.</p>
  
  <p>I'm using following code:</p>
  
  <pre><code>ofstream writeToTempFile;
  ifstream readFromTempFile;
  
  writeToTempFile.open("tempFile.txt", ios::app | ios::out);
  readFromTempFile.open("tempFile.txt", ios::in); 
  
  // Reading and Appending data to the file
  // Now it is time to erase all the previous data and start writing from the beginning
  
  
  writeToTempFile.open("tempFile.txt", std::ofstream::trunc); // Here I'm removing the contents.
  
  // Write some data to the file
  
  writeToTempFile.open("tempFile.txt", std::ofstream::app); // Using this, I'm again having my file in append mode
  </code></pre>
  
  <p>But what I've done doesn't work correctly. Please suggest me some solution in C++. ( Not in C)</p>