Michael Dorner
Michael Dorner

Reputation: 20165

What happens to open files which are not properly closed?

What happens if I do not close a file after writing to it?

Let us assume we got an too many open files error and due to that the program crashes.

Does the OS handle that for me? And if this damages the not-closed files, how do I notice that they are damaged?

Upvotes: 5

Views: 1561

Answers (2)

user3344003
user3344003

Reputation: 21637

Generally speaking, if you write to a file, then your application crashes, the operating system will flush the buffers to the disk and clean up for you. The same will occur if your program exits without explicitly closing the files. This does not damage files.

The bad situation is when you write to a file and someone pulls the plug out on the computer.

Upvotes: 2

Tony Tannous
Tony Tannous

Reputation: 14851

From exit()

_exit() does close open file descriptors, and this may cause an unknown delay, waiting for pending output to finish.

Each return hides a system call to exit, so any unclosed descriptor is closed by the OS.

Upvotes: 3

Related Questions