+ 3

What will happen if you forget to close a file after reading/writing in C++?

19th Sep 2020, 11:46 PM
lehlohonolo hlox
lehlohonolo hlox - avatar
4 odpowiedzi
+ 12
Since the ifstream destructor is called when the program ends (or whenever the instance is destroyed), close() is also invoked. https://en.cppreference.com/w/cpp/io/basic_fstream/close However, it is a good practice to always close opened files after you are done writing to them. In a large program, which runs on long after you have finished reading/writing to the file, not closing it means that your program is still holding the resource. This means that other processes cannot acquire that file until your program terminates (which may not be what you wish for).
20th Sep 2020, 1:40 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Hatsy Rei Thx for the info and the tip. I never considered that this could be a thread actually. Anyway I always did in my active programming life. So you see your never to old to learn something new. 🤔👍
20th Sep 2020, 1:42 PM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 2
Arshia You are missing the point. As long as you don't close the opened data file no one else is getting access to the same file to Make changes or add information.
21st Sep 2020, 10:34 AM
🇺🇸 Anatoli🇧🇪🇪🇺 ,
🇺🇸 Anatoli🇧🇪🇪🇺 , - avatar
+ 1
Nothing happens
21st Sep 2020, 6:29 AM
Arshia
Arshia - avatar