consider the below code snippet.
{
....
FILE *fptr = fopen("file_that_does_not_exist","r");
...
}
here, if fopen fails, do we still need to call fclose() for some cleanup ??
As long as your program is running, if you keep opening files without closing them, the most likely result is that you will run out of file descriptors/handles available for your process, and attempting to open more files will fail eventually.
"fopen" opens a file for subsequent reading or writing. If successful, "fopen" returns a pointer-to-structure; if it fails, it returns NULL.
Calling fclose will ensure the file descriptor is properly disposed of and output buffers flushed so the data written to the file will be present in the file on disk.
The fopen() function is used to open the file or an URL. The fclose() function closes the opened file.
No, since fopen() returns 0, which is an invalid file handle. fclose() may even crash if you try to do this.
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