What happens if I open a file and I do not close it?


I am new to programming in ansi C. I am currently learning to manipulate files.

My question is if I open a file using the function fopen, and I finish the program without using fclose, the file can be damaged or is there a pending process on the PC or just nothing happens?

Thank you very much in advance.

asked by Jose Alberto Rosa Mañan 01.06.2018 в 02:09

1 answer


The result is dependent on the Operating System.

When you acquire a resource (a file, a socket, ...) the system reserves you this resource offering you an exclusive access to it. Nobody else will be able to access this resource while you have it booked.

If the required access is read only, the system can offer shared access to the resource as long as all accesses are read.

If you acquire a resource but do not release it, the system maintains the reservation of it, so you prevent another application from accessing it.

What usually happens (at least in modern operating systems) is that when you close your application the resources associated with that application are automatically released ... but it does not necessarily have to be that way. If it turns out that the resources are not released they will be blocked indefinitely (because your application will no longer be able to release them when you lose their identifiers) and the only solution will be to restart the computer.

You have to be especially careful with remote resources, since then it is quite likely that the remote operating system has no record that your application has been closed, so the possibility of resources being blocked increases significantly.

answered by 01.06.2018 / 09:42