I have a section of code that repeats many lines and I am looking to refactor this code possibly so that a function can be used instead of the same lines of code each time. Below is the section of code and the repeating code occurs in each if statement
Traversal::Traversal(string source, bool isFileName)
{
using namespace XML_Parser;
using std::endl;
std::ostringstream ppd;
if (isFileName)
{
string content, line;
std::ifstream file(source);
if (! file)
{
ppd << endl << "error opening file " << source;
cRep = ppd.str();
cSuc = false;
return;
}
ppd << endl << " file " << source << " opened ";
while (file.good())
{
getline(file, line);
content += '\n';
content += line;
}
ppd << endl << "file " << source << " read ";
source = content;
}
if (! elementExists(source,"afcd"))
{
ppd << endl << "no afcd tag";
cRep = ppd.str();
cSuc = false;
return;
}
if (! elementExists(source,"rteirl"))
{
ppd << endl << "no rteirl tag";
cRep = ppd.str();
cSuc = false;
return;
}
How would I go about making this cleaner?
while (file.good())
is wrong for the same reasons thatwhile (!file.eof())
is. \$\endgroup\$