Do opened files close automatically when they go out of scope?

I understand that explicitness is very useful for reading complicated codes (much later, or written by other people). In this respect, I feel the Fortran language is designed more for domain experts than for people with a lot of CS expertise. Nevertheless, sometimes I meet some code patterns which are too obvious to write (or even read), one of which is the close statements at the end of the program…

By the way, I really like the file type suggested above (with a final routine that closes the file automatically), but I guess the behavior of the file objects might be less trivial (compared to explicit close) if the user is not used to the final routine.

(FWIW, I have not used final up to now because I have seen a lot of issue reports, if I remember correctly… If the final is okay to use, I would like to try it in my codes also.)

Yes, I liked that use of final too and have written my own implementation since your question provoked such a useful response. FWIW I have only ever encountered one issue with final and that is what a debugger does if you are stepping through code with say F11. Intel would step over final routines so you could print “goodbye” in the final routine but never step into the line that produced the message. I raised it as an issue, but was told it was a feature.
Good luck

1 Like

If you go back 30 or 40 years when memory, and even address space, was limited, then programmers would always close files that were no longer needed. Or sometimes we would even close files that might need to be reopened later just to save memory. In addition to the file structure itself (file characteristics, file pointer, etc.), there are also some internal i/o buffers associated with the file. If you left a bunch of files open simultaneously, they would take up all of the available memory. These days, memory is not so precious, and programmers just throw it around like it is nothing and never really worry about it.

The main reason these days for closing files is probably related to file integrity. Each file, say a network file of some kind, is associated with disk space on the rotating medium. Then the disk device itself often has DRAM memory that it uses internally for staging the i/o operations. In “fusion” drives, which can have TB of SRAM, there can be three levels of memory on the device itself. Then the network transport system has its own buffers for the server and client nodes and sometimes for intermediate nodes, depending on the network topography. Then the client program has its i/o buffers within the fortran i/o library. And the user program itself might have its own buffers, particularly for asynchronous i/o operations. So when a file is closed, lots of stuff needs to happen to get those bits from the program running on the client all the way to the spinning hard drive surface. If a program crashes in some unexpected way, then some of those flush and close operations might not occur, so data would be lost unnecessarily. Adding explicit close statements helps to protect that data to some extent.

2 Likes