QVector or 2D Array to read out from a file
Hi,
i got a file which holds some lines like this:
19,sunshine live,0x1234
19,radio bob, 0xabcd
19,sputnik,0x1a2b
and so on..
My program reads this file and my goal is to output only the station name (2 entry in each line) in a QListWidget. Anyway the other 2 entries in each line should be hold in a vector or array, to use them later.
So far I got this, but it doesn't work right:
Code:
void MainWindow::on_pushButton_clicked()
{
QMessageBox::warning(this,
"..",
"keine datei gefunden");
return;
}
QVector<QVector<QString> > vectorOfVectorsOfStrings;
QVector<QString> zeile;
while (!in_file.atEnd()) {
text = in_file.readLine();
for (int i = 0; i < 3; i++){
zeile.push_back(split_text.at(i));
vectorOfVectorsOfStrings.push_back(zeile);
}
}
file.close();
for(int i = 0; i < vectorOfVectorsOfStrings.size(); i++)
{
for(int j = 0; j < vectorOfVectorsOfStrings[i].size(); j++)
{
ui->listWidget->addItem(vectorOfVectorsOfStrings[i][1]);
}
}
}
The program is crashing with message [i] out of range. So i tried to set a fixed value just to so the output, and then my ListWidget was showing several times sunshine live. I guess the error is somewhere in here:
Code:
while (!in_file.atEnd()) {
text = in_file.readLine();
for (int i = 0; i < 3; i++){
zeile.push_back(split_text.at(i));
vectorOfVectorsOfStrings.push_back(zeile);
}
}
Re: QVector or 2D Array to read out from a file
I would add a check on the number of items in split_text list.
Re: QVector or 2D Array to read out from a file
Quote:
The program is crashing with message [i] out of range.
Then use a debugger and take a look at the backtrace why the assertion occours.
Re: QVector or 2D Array to read out from a file
So I added a debug to the while statement, to see what is hold in vectorOfVectorsOfStrings
Code:
QVector(QVector("19"),
QVector("sunshine live"),
QVector("0x1234"),
QVector("19"),
QVector("dlf kultur"),
QVector("0x9876"),
QVector("19"),
QVector("dlf1 kultur"),
QVector("0x9876"),
QVector("19"),
QVector("dlf2 kultur"),
QVector("0x9876"),
QVector("19"),
QVector("dlf3 kultur"),
QVector("0x9876"),
QVector("19"),
QVector("dlf4 kultur"),
QVector("0x9876"),
QVector("19"),
QVector("dlf5 kultur"),
QVector("0x9877"))
I expected something like
Am I wrong and it already runs well, or am I right?
Added after 1 10 minutes:
Just to complete it, solution:
Code:
void MainWindow::on_pushButton_clicked()
{
QMessageBox::warning(this,
"..",
"keine datei gefunden");
return;
}
QVector<QVector<QString>> vectorOfVectorsOfStrings;
QVector<QString> zeile;
while (!in_file.atEnd()) {
text = in_file.readLine();
qDebug() << text;
qDebug() << split_text;
QVector<QString> zeile;
zeile.push_back(split_text.at(0));
zeile.push_back(split_text.at(1));
zeile.push_back(split_text.at(2));
qDebug() << zeile;
vectorOfVectorsOfStrings.push_back(zeile);
qDebug() << vectorOfVectorsOfStrings;
}
file.close();
for(int i = 0; i < vectorOfVectorsOfStrings.size(); i++)
{
ui->listWidget->addItem(vectorOfVectorsOfStrings[i][1]);
}
}
Re: QVector or 2D Array to read out from a file
For one thing, you have declared the variable "zeile" twice, once outside your loop, and again inside it. The inside declaration hides the outer one.