c++ - Qt - record from .cvs file causing errors when pushed into vector -
i writing program fills qtableview data cvs file. pushing data vector of objects class. want able catch record file may cause out of bound error , move onto next line without adding record table or vector.
here function populates table:
qstring filename = "currentstudents.csv"; qfile file(filename); file.open(qiodevice::readonly); int lineindex = 0; qtextstream input(&file); while (!input.atend()) { std::vector<qstring> newrecord; qstring fileline = input.readline(); qstringlist linetoken = fileline.split(",", qstring::skipemptyparts); (int = 0; < linetoken.size(); i++) { qstring value = linetoken.at(i); newrecord.push_back(value); //this line out of bounds error comes qstandarditem *item = new qstandarditem(value); currentstudentsmodel->setitem(lineindex, i, item); } try { //creating student object information parsed file currentstudent student(newrecord.at(0), newrecord.at(1), newrecord.at(2).toint(), newrecord.at(3).toint(), newrecord.at(4).toint(), newrecord.at(5).touint()); currentstudents.push_back(student); } catch (const std::out_of_range& e) { qdebug() << "out of range error: " << e.what(); } lineindex++; }
if record in file formatted in weird way such (,,,,,,) or without 6 commas separate data, load record table improperly , cause out of bounds error eventually. how program skip on problematic line , move onto next?
you add validation test:
while (!input.atend()) { // ... (int = 0; < linetoken.size(); i++) { // load newrecord... } // validate.. if (newrecord.size() != 6) { // print warning here ? // skip continue; } if (newrecord.at(0) == "") // example... { // print warning here ? // skip continue; } // more validation ? // add record currentstudent student(newrecord.at(0), newrecord.at(1), newrecord.at(2).toint(), newrecord.at(3).toint(), newrecord.at(4).toint(), newrecord.at(5).touint()); currentstudents.push_back(student); } // end loop
Comments
Post a Comment