fwrite()
is a library call that firstly buffers the data into a user space buffer, and then calls the write()
system call later to actually carry out the write operations.
If a program invokes
fwrite()
to write some data to a file but then exists abnormally, will the buffer offwrite()
beclearedflushed, or the buffered data will be left over in memory?
The OS I am considering is Linux.
NOTES top. Note that fclose() flushes only the user-space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too, for example, with sync(2) or fsync(2).
The ANSI/ISO fread/fwrite functions are buffered.
If your program exited abnormally, any buffered data will not be flushed. The OS just says "oh dear me, you left a file descriptor open, I better close that for you" when the process terminates; it has no idea there's some random data lying somewhere in memory that the program intended to write to disk but did not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With