title: Why using fread after putw expands the file in C?
body: <p>I was trying to read some data from a file using <code>fread()</code>, and I realized that my file keeps growing and growing. But since I was reading from a file, the behavior was not reasonable for me. So I wrote this code and found that if I use <code>putw()</code> to write data to a file, then try to read from that file(before closing and reopening the file), <code>fread</code> expands the file to be able to read from it.</p>
  <p>Operating System: Windows 8.1<br />
  Compiler: MinGW gcc</p>
  <p>The code:</p>
  <pre><code>typedef struct {
      int a;
      int b;
  } A;
  
  int main() {
      FILE* f = fopen(&quot;file&quot;, &quot;wb&quot;);
      A a;
      a.a = 2;
      a.b = 3;
      putw(1, f);
      fwrite(&amp;a, sizeof(A), 1, f);
      fclose(f); // To make sure that wb mode and fwrite are not responsible
      f = fopen(&quot;file&quot;, &quot;rb+&quot;);
      printf(&quot;initial position: %ld\n&quot;, ftell(f));
      putw(1, f);
      printf(&quot;position after putw: %ld\n&quot;, ftell(f));
      printf(&quot;fread result: %d\n&quot;, fread(&amp;a, sizeof(A), 1, f));
      printf(&quot;position after 1st fread: %ld\n&quot;, ftell(f));
      printf(&quot;fread result: %d\n&quot;, fread(&amp;a, sizeof(A), 1, f));
      printf(&quot;position after 2nd fread: %ld\n&quot;, ftell(f));
      fclose(f);
      remove(&quot;file&quot;);
      return 0;
  }
  </code></pre>
  <p>RESULT:</p>
  <pre class="lang-none prettyprint-override"><code>initial position: 0
  position after putw: 4
  fread result: 1
  position after 1st fread: 12
  fread result: 1
  position after 2nd fread: 20
  </code></pre>