Versions Compared

Key

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

...

Code Block
bgColor#ffcccc
langcpp
#include <fstream>
#include <string>

void f(const std::string &Nfile_name) {
  std::fstream ffile(Nfile_name);
  if (!ffile.is_open()) {
    // Handle error
    return;
  }
  
  ffile << "Output some data";
  std::string str;
  ffile >> str;
}

Compliant Solution

...

Code Block
bgColor#ccccff
langcpp
#include <fstream>
#include <string>

void f(const std::string &Nfile_name) {
  std::fstream ffile(Nfile_name);
  if (!ffile.is_open()) {
    // Handle error
    return;
  }
  
  ffile << "Output some data";
 
  std::string str;
  ffile.seekg(0, std::ios::beg);
  ffile >> str;
}

Risk Assessment

Alternately inputting and outputting from a stream without an intervening flush or positioning call is undefined behavior.

...