title: How can I determine the current size of the file opened by std::ofstream?
body: <p>I have a class that has a filestream of type <code>ofstream</code>. The constructor opens the file in append mode and all the messages always get written at the end of the file.</p>
  
  <p>I need to write into outputFile up to some fixed size say 1Mb, then I need to close, rename, and compress it, and then open a new file of the same name.</p>
  
  <p>This needs to be done when a certain size of file is reached.</p>
  
  <p>I tried using <code>tellg()</code> but after reading stuffs (and <a href="https://stackoverflow.com/questions/22984956/tellg-function-give-wrong-size-of-file">this</a>) on internet, I understood that this is not the right approach.</p>
  
  <p>As I'm new to C++, I'm trying to find out the most optimized and correct way to get the accurate current size of file opened by <code>ofstream</code>?</p>
  
  <pre><code>class Logger {
      std::ofstream outputFile;
      int curr_size;
      Logger (const std::string logfile) : outputFile(FILENAME,
                                                       std::ios::app)
      {
          curr_size = 0;
      }
  };
  </code></pre>
  
  <p>Somewhere in the program, I'm writing data into it:</p>
  
  <pre><code>    // ??? Determine the size of current file ???
  
      if (curr_size &gt;= MAX_FILE_SIZE) {
          outputFile.close();
          //Code to rename and compress file
          // ...
          outputFile.open(FILENAME, std::ios::app);
          curr_size = 0;
      }
  
      outputFile &lt;&lt; message &lt;&lt; std::endl;
      outputFile.flush();
  </code></pre>