Reputation: 3757
Say I'm writing to a file but I want to end the write by turning off the power to the machine, would that be a problem that the fclose() function didn't get called?
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* print some text */
while(1){
//write something...
}
//Dont want to call this
fclose(f);
Upvotes: 0
Views: 518
Reputation: 63471
Short answer: no, not as written, if there is any kind of buffering. And if no buffering, also maybe no.
The buffering mode can be controlled with setvbuf
. You can also periodically call fflush
to flush the buffers.
However, regardless of how you manage the buffering, control is transferred to the OS kernel I/O scheduling, and then to the hardware (which may have its own buffering).
The file I/O routines in <stdio.h>
, to my knowledge, don't give you control at a low enough level to be absolutely certain your data is written upon return from a call such as fflush
.
Note that here I'm talking about sudden unexpected power loss.
Upvotes: 0