2017-07-28 2 views
0

私は、cvsファイルのデータをQTableViewに書き込むプログラムを作成しています。また、そのデータをクラスのオブジェクトのベクトルにプッシュします。私は、ファイルからレコードをキャッチして、範囲外のエラーを引き起こし、そのレコードをテーブルやベクトルに追加せずに次の行に移動できるようにしたい。ファイル内のレコードがデータを分離するために、このような(,,,,,,)などの任意の奇妙な方法で、又は6つのカンマなしでフォーマットされている場合Qt - ベクトルにプッシュするとエラーが発生する.cvsファイルのレコード

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); 

    for (int i = 0; i < lineToken.size(); i++) { 

     QString value = lineToken.at(i); 
     newRecord.push_back(value);  //this line is where out of bounds error comes from 
     QStandardItem *item = new QStandardItem(value); 
     currentStudentsModel->setItem(lineindex, i, item); 
    } 

    try 
    { 
     //creating a student object with the information parsed from the 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++; 
} 

:ここ

テーブルを移入関数でありますテーブルに不適切にレコードをロードし、最終的には範囲外のエラーが発生します。問題のある行をスキップして次の行に移動するにはどうすればよいですか?あなたが検証テストを追加することができます

答えて

0

while (!input.atEnd()) 
{ 
    // ... 
    for (int i = 0; i < lineToken.size(); i++) { 
     // load newRecord... 
    } 
    // validate.. 
    if (newRecord.size() != 6) 
    { 
     // print warning here ? 
     // skip 
     continue; 
    } 
    if (newRecord.at(0) == "") // for 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 
+0

おかげで、これは私が仕事をするのtry catchブロックを取得するために失敗した後、使用してしまった方法です。 – stor314