+ 3
What will happen if you forget to close a file after reading/writing in C++?
4 Antworten
+ 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).
+ 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. 🤔👍
+ 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.
+ 1
Nothing happens