title: Difference between fflush and fsync
body: <p>I thought <code>fsync()</code> does <code>fflush()</code> internally, so using <code>fsync()</code> on a stream is OK. But I am getting an unexpected result when executed under network I/O.</p>
  
  <p>My code snippet:</p>
  
  <pre><code>FILE* fp = fopen(file, "wb");
  /* multiple fputs() calls like: */
  fputs(buf, fp);
  ...
  ...
  fputs(buf.c_str(), fp);
  /* get fd of the FILE pointer */
  fd = fileno(fp);
  #ifndef WIN32
  ret = fsync(fd);
  #else
  ret = _commit(fd);
  fclose(fp);
  </code></pre>
  
  <p>But it seems <code>_commit()</code> is not flushing the data (I tried on Windows and the data was written on a Linux exported filesystem).</p>
  
  <p>When I changed the code to be:</p>
  
  <pre><code>FILE* fp = fopen(file, "wb");
  /* multiple fputs() calls like: */
  fputs(buf, fp);   
  ...   
  ...
  fputs(buf.c_str(), fp);
  /* fflush the data */
  fflush(fp);
  fclose(fp);
  </code></pre>
  
  <p>it flushes the data.</p>
  
  <p>I am wondering if <code>_commit()</code> does the same thing as <code>fflush()</code>. Any inputs?</p>