fclose function in c, Why fread() works but not fwrite() if you call fclose for file handler

StackOverflow https://stackoverflow.com/questions/19062239

  •  29-06-2022
  •  | 
  •  

Question

Why is it if we read a file with fread() and don't include fclose to close the same same file reference but still it works.

However if we forgot to include fclose after fwrite then it doesn't work.i.e. no writing on file reflects.

Was it helpful?

Solution

fread(), as the name implies, reads data from a file. Logically, it can't return until it's either actually read the data (stored it in the buffer you provided) or reported an error. As soon as fread() returns, you can use the data.

fwrite(), on the other hand, can go through multiple layers of buffering, in the C runtime library, in the OS, and elsewhere. fwrite() returns when it's processed your data and written it ... somewhere. There's no guarantee that it's actually written to the physical file until you either flush it (fflush(outfile);) or close it (fclose(outfile);). As soon as fwrite() returns, you can do what you like with the data you passed to it (it's been copied), but that's all you can assume.

If your program terminates normally, it will implicitly close (and therefore flush) all open output files. If your program terminates abnormally, this may not happen.

(Actually, I think there are some cases where data may not physically arrive in the file even after fclose() or fflush().)

OTHER TIPS

File access is buffered. When you execute an fread(), it can't return until the actual data has been read, but if you execute an fwrite() then it can return after placing the written data in the file buffer but before writing it to the underlying file.

A side-effect of fclose() is flushing the file buffers - you could also use fflush() after fwrite() to achieve the same effect.

Because the read operation must obviously complete after the fread function returned - having actually read the data in your buffer is one of the postconditions of fread, otherwise you couldn't be sure that you can do anything useful with the data in the buffer after calling fread1.

fwrite, instead, can (and will) do write buffering - it will accumulate data in its private buffer before passing it to the operating system to actually write it down in the file; this is possible (and convenient) since, as far as your program is concerned, this thing is completely transparent: the data has already been stored in some other buffer, if you read back from the same stream you'll find your data there, and still you get the performance improvements from the caching. Still, if you need your data to go immediately to the OS, you can use fflush.

Incidentally, even if you don't call fclose right away the standard guarantees that, in case of normal program termination, the streams are automatically closed, and thus their content is flushed.


  1. By the way, there are instead scenarios where is useful to get data only if it's available or to start read operations and check later if the data have been actually read - and that's why operating systems provide nonblocking and asynchronous IO functions.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top