2

I understand that not closing a file is irresponsible, but under the following two conditions, what issues can arise if fclose() was not be called?

1) In the event that fclose() was not called when a program ends, does the OS communicate with the file descriptor to let it know that the file is not longer needed and closes it?

#include <stdio.h>
int main(){
    FILE* f = open("sample.txt","r");
    return 0;
};

In the event of a explicit exit e.g. exit(1) does it differ in any way as to just returning in the previous example?

#include <stdio.h>
int main(){
    FILE* f = fopen("sample.txt","r");
    exit(1);
    return 0;
}

2) A potential segmentation fault occurs before fclose() could be called?

#include <stdio.h>
int main(){
    FILE* f = fopen("sample.txt","r");
    int *ptr = NULL;
    *ptr = 1; //seg-fault
    fclose(f);
    return 0;
};
Miket25
  • 1,895
  • 3
  • 15
  • 29
  • Corrupted data inside of the file is most likely the answer. I've had a java file that created pdf's that failed before file close and when I tried to open the .pdf file it wouldn't let me, stating the data was corrupted. I'd try to catch any potential stops to the program to properly close the file before exit. – J0hn Jul 07 '17 at 15:42
  • for 'normal' OSes, if the file is opened read only nothing bad should happen. But if you have written to the file, bufferd data will most likley not be written – Ingo Leonhardt Jul 07 '17 at 15:44
  • In C, a return from `main()` has in every way exactly the same effects as passing the erstwhile return value as the argument to an `exit()` call. – John Bollinger Jul 07 '17 at 16:28

2 Answers2

2

If the program has a seg fault, the file handler will eventually be released by the OS, just like when program exits normally. But I have seen cases where the file handler from the seg'ed program was not available right away.

More details on a previous answer What happens if I don't call fclose() in a C program?

gmwagner
  • 71
  • 4
  • When you wrote "the file handler will eventually be released", did you mean the entries in the open file tables will be deleted? When you wrote "where the file handler ... was not available right away", did you mean file could not be opened by another process right away? – Jeff Holt Jul 07 '17 at 16:01
  • (1) I meant the file descriptor in C will become available. And as pointed out in [another answer](https://stackoverflow.com/questions/7370363/what-happens-if-you-exit-a-program-without-doing-fclose) when there is a non-exit() close, the buffers won't be flushed. (2) The file descriptor of a seg'ed program was sometimes not available, but the actual file was available. – gmwagner Jul 07 '17 at 16:14
  • "file descriptor in C" You mean the offset in the open files tables won't be reusable for the process that got sent the signal. In other words, if your code handles the signal and, say, the open file descriptor is 5, then 5 won't be reused for some time interval? – Jeff Holt Jul 07 '17 at 16:18
  • 1
    Correct, that's what I meant – gmwagner Jul 07 '17 at 16:19
0

If you use exist, in C Language, the exit function calls all functions registered with atexit and terminates the program. File buffers are flushed, streams are closed, and temporary files are deleted.

And even you don't the OS should release all the program resources once it terminates. Though not cleaning up is a bad habit that can lead to serious issues.

Rand0m
  • 342
  • 1
  • 4
  • 10