Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Coding style conformance

...

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
bgColor#FFcccc
langcpp
#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
bgColor#ccccff
langcpp
#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();
}

...