...
In this noncompliant code example, a std::fstream
object f
file
is constructed. The constructor for std::fstream
calls std::basic_filebuf<T>::open()
, and the default std::terminate_handler
called by std::terminate()
is std::abort()
, which does not call destructors. Consequently, the underlying std::basic_filebuf<T>
object maintained by the object is not properly closed, and the program has no way of determining whether an error occurs while flushing or closing the file.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <exception> #include <fstream> #include <string> void f(const std::string &NfileName) { std::fstream ffile(NfileName); if (!ffile.is_open()) { // Handle error return; } // ... std::terminate(); } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
#include <exception> #include <fstream> #include <string> void f(const std::string &NfileName) { std::fstream ffile(NfileName); if (!ffile.is_open()) { // Handle error return; } // ... ffile.close(); if (ffile.fail()) { // Handle error } std::terminate(); } |
...