c++ - do I need to close a std::fstream? -
possible duplicate:
do need manually close ifstream?
do need call fstream.close()
or fstream
proper raii object closes stream on destruction?
i have local std::ofstream
object inside method. can assume file closed after exiting method without calling close? not find documentation of destructor.
i think previous answers misleading.
fstream
is proper raii object, does close automatically @ end of scope, , there absolutely no need whatsoever call close
manually when closing @ end of scope sufficient.
in particular, it’s not “best practice” , it’s not necessary flush output.
and while drakosha right calling close
gives possibility check fail bit of stream, nobody that, anyway.
in ideal world, 1 call stream.exception(ios::failbit)
beforehand , handle exception thrown in fstream
’s destructor. unfortunately exceptions in destructors broken concept in c++ that’s not idea.
so if want check success of closing file, manually (but then).
Comments
Post a Comment