Why must one close the file? What happens if a file is not closed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why must one close the file? What happens if a file is not closed?

7th Dec 2017, 4:54 PM
Zach
2 Answers
+ 2
First and possibly the most important reason is that the changes might not be saved. In the event of a blackout if the file is not closed the changes won't be saved(for example). Second you will use resources for no reason, for a small app with a short lifespan it's not a big deal with resources, but that is bad practice and it's no excuse for that. Third, lets say you have 2 programs that need to open the same file(or a multi-thread program). If in both programs you don't close the file the second program that tries to open the file will crash because the file is used by another program.
7th Dec 2017, 5:35 PM
bogdan
bogdan - avatar
+ 2
closing a file is when it saves, also another reason is if you have a program that's going to run for a long time you don't want to keep opening files and have then taking up resources and you can only have to many files open so eventually you will run into problems. if it's a program that is going to run and end it's not as big a deal. just not sure how saving will work.
7th Dec 2017, 5:21 PM
Mooaholic
Mooaholic - avatar