title: why fclose() is not always flushing the data to the disk?
body: <p>I'm getting some weird results, while trying to write data to files in <code>C.&lt;br/&gt;</code>
  I thought that <code>fclose()</code> closes the <code>*FILE</code> and <code>flushes</code> the data from its buffer to the <code>file.&lt;br/&gt;</code>
  But for some reason it only flushes the data in my program sometimes and it doesn't do it  other times.</p>
  <p>For example: I would run this code, and in my file I can see the two strings. (Perfect, exactly what I want)<br/>
  But then when I run the code the next 4 times it doesn't change anything in my file. And then when I run it another time, suddenly the 10 extra strings appear (8 from the last times I ran the program and the 2 from now)<br/>
  (This 4 times is just an example, sometimes it's 5, 8, 10, or even just 2 times before I see the output appear)</p>
  <p>I really don't understand this? <strong>Shouldn't the data be visible after every time I run the program?</strong> Where is this buffer even saved between the different times I run the program, because the program finishes every time, so the memory gets released, right?</p>
  <p>(By the way I also tried <code>fflush(fd)</code> before and after fclose(), but that didn't solve the problem)</p>
  <pre><code>#include &lt;stdio.h&gt;
  
  
  int main(int argc, char const *argv[]) {
    FILE * fd;
    fd = fopen(&quot;test_file.txt&quot;, &quot;a&quot;);
    fprintf(fd, &quot;String 1\n&quot;);
    fprintf(fd, &quot;String 2\n&quot;);
    fclose(fd);
    return 0;
  }
  </code></pre>