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

**URL:** <https://fortran-lang.discourse.group/t/do-opened-files-close-automatically-when-they-go-out-of-scope/4065>\
**Category:** Uncategorized\
**Created:** [July 25, 2022, 4:57am UTC](https://fortran-lang.discourse.group/t/do-opened-files-close-automatically-when-they-go-out-of-scope/4065 "2022-07-25T04:57:31Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [July 31, 2022, 1:48pm UTC](https://fortran-lang.discourse.group/t/do-opened-files-close-automatically-when-they-go-out-of-scope/4065/22 "2022-07-31T13:48:46Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![NormanKirkby](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/normankirkby/32/1091_2.png) [@NormanKirkby](https://fortran-lang.discourse.group/u/NormanKirkby)\
**Post date:** [July 31, 2022, 2:25pm UTC](https://fortran-lang.discourse.group/t/do-opened-files-close-automatically-when-they-go-out-of-scope/4065/23 "2022-07-31T14:25:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RonShepard](https://avatars.discourse-cdn.com/v4/letter/r/a3d4f5/32.png) [@RonShepard](https://fortran-lang.discourse.group/u/RonShepard)\
**Post date:** [July 31, 2022, 5:02pm UTC](https://fortran-lang.discourse.group/t/do-opened-files-close-automatically-when-they-go-out-of-scope/4065/24 "2022-07-31T17:02:10Z")

</div>

> [@septc](#):
>
> Thanks very much for the info! Because I was not sure about this, I always wrote explicit `close` statements even for files opened throughout the program. Assuming the OS closes those files automatically, do you think it is not particularly risky to omit such `close` statements? (Of course, if files are used for a given scope or a given time in the program, I will explicitly close them.)

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.

[Previous page](https://fortran-lang.discourse.group/t/do-opened-files-close-automatically-when-they-go-out-of-scope/4065.md?page=1)
