2013-04-20 61 views
0
QVector <QString> frequency; 
QVector <QString> gain; 
QString ref_data; 
QStringList rowOfData; 
QStringList rowData; 

if (ReferenceData.open(QIODevice::ReadOnly | QIODevice::Text)) { 
    ref_data = ReferenceData.readAll(); 
    rowOfData = ref_data.split("\n"); 
    ReferenceData.close(); 
} 

for (int x = 9; x < rowOfData.size(); x++) { 
    rowData = rowOfData.at(x).split(","); 
    frequency.push_back(rowData[0]); 
    gain.push_back(rowData[1]); 
} 

我從文件中讀取所有數據。當我使用push_back時,出現錯誤QList<T>::operator[] "index out of range"。我也用frequency.push_back(rowData.at(0));,然後我得到QList<T>::at "index out of range"錯誤!從csv文件插入值到Qt向量時出錯

如何解決這個問題?

+0

什麼是rowData,頻率和增益? – 2013-04-20 20:23:27

+0

我將編輯 – 2013-04-20 20:26:40

+1

您是否在調試器中運行代碼?你檢查過'rowOfData.at(x)'的值嗎?你是否檢查過'rowData'確實包含至少兩個值? – 2013-04-20 20:29:27

回答

2

後期循環應該處理不好線,例如像這樣的,只是跳過他們與調試打印:

for (int x = 9; x < rowOfData.size(); x++) { 
    rowData = rowOfData.at(x).split(","); 
    if (rowData.size() >= 2) { 
     frequency.push_back(rowData[0]); 
     gain.push_back(rowData[1]); 
    } 
    else qDebug() << "Skipping bad row" << x << rowOfData.at(x); 
}