I have seen both "yes" and "no" as an answer to this. Most examples would show me
Yet fstream has close() on its destructor (it is C++....) and if you really want to close it at a specific point it seems better to use a scope bracket as it is cleaner and less bug-prone.Code:ifstream ifs ("mytest.txt"); //do something ifstream.close();
On the other hand if you don't close() it manually you cannot check the failbit and potentially miss an error. Yet, in most (if not all) examples I see there is never a check on close(). If you were to check also for failures usually you are worried of losing data. In which case maybe you prefer to call flush() and in any case you could wrap up the fstream in a class that handles the error appropriately without require manual releasing of resources.Code://some code { ifstream ifs ("mytest.txt"); //do something } //some code
What is the common practice for this?
As a note, if I search on MSDN site for file stream in Visual C++ I see example with ifstream and close(). Yet if I search for C# I see examples where the "using" schema with a stream reader is used, which does essentially what a C++ destrcutor does (apart from the memory release part)!