0

I have opened a file for writing some data to a file, but it keeps buffering and won't write anything to the file until the program ends. How can I skip the buffering process and write directly to the file.

My code:

fprintf (fp, "# Step: %d  %f\n", ntot, eDiff);
for(i=0; i<nType; i++)  
    fprintf (fp, "%s  %f %f\n",param[i].cTypes, param[i].eps, param[i].sigma);
fprintf(fp, "#  ============\n");
Vahid Mirjalili
  • 6,211
  • 15
  • 57
  • 80
  • It may be the OS that is buffering in some weird way, if there is any buffering at all. There is something that may help -> http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin – Etherealone May 15 '13 at 21:38
  • You're missing the fopen, fsync, and fclose calls. You're even missing main(). How do you expect us to help you? – CAFxX May 15 '13 at 21:39
  • 1
    use `setvbuf` buffering control. – BLUEPIXY May 15 '13 at 21:45

1 Answers1

4

Use the fflush() function on the stream:

fflush( fp );
svk
  • 5,854
  • 17
  • 22