This usually depends on which file you are interacting with. Of course, binaries should be open while reading. You can parse text into a string and close the file immediately if the file is plain text.
I would like to say that you should keep the file pointer open for the entire life of the program, but if you encounter a crash, it will never be closed. Then I suggest leaving it open during relatively safe operations and low-risk operations, but always open / close it when you partially relate to the stability of the application. No one wants file pointers to remain open. :)
source share