...
Code Block | ||||
---|---|---|---|---|
| ||||
#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 | ||||
---|---|---|---|---|
| ||||
#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.
...