title: How can I determine if a file is empty?
body: <p>How do I determine if a file is empty? The file is opened by a C program running on the Windows platform. I want to open a file in append mode, and, if empty, first print a header to it.</p>
  
  <pre><code>// Open CSV &amp; write header
  report_csv = fopen("SNR.csv", "a+");
  if (!report_csv) {
      fprintf(stderr, "Unable to open CSV output file...");
      return -1;
  }
  if (!ftell(report_csv)) {
      fprintf(report_csv, "Column A;Column B;Column C\n");
  }
  // ... print data to file
  fclose(report_csv);
  </code></pre>
  
  <p>I was expecting <code>ftell</code> to return the current file size if the file was not empty, which happens because the code above is looped.  </p>
  
  <p>However, <code>ftell</code> always returns <code>0</code> and the header is printed multiple times.</p>
  
  <p>I know I could <code>fopen</code> it with <code>r</code> and use <code>fseek</code>/<code>ftell</code>/<code>fclose</code> and then <code>fopen</code> it again with <code>a+</code>, but I think it's possible to do this without opening and closing the file multiple times.</p>