PDA

View Full Version : QVector or 2D Array to read out from a file



vitalic
22nd April 2020, 15:14
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:



void MainWindow::on_pushButton_clicked()
{
QFile file(pfad);
if(!file.open(QFile::ReadOnly | QFile::Text)){
QMessageBox::warning(this,"..","keine datei gefunden");
return;
}

QTextStream in_file(&file);
QString text;

QVector<QVector<QString> > vectorOfVectorsOfStrings;
QVector<QString> zeile;

while (!in_file.atEnd()) {
text = in_file.readLine();
QStringList split_text = text.split(",");

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:



while (!in_file.atEnd()) {
text = in_file.readLine();
QStringList split_text = text.split(",");

for (int i = 0; i < 3; i++){

zeile.push_back(split_text.at(i));
vectorOfVectorsOfStrings.push_back(zeile);
}
}

Lesiok
22nd April 2020, 17:09
I would add a check on the number of items in split_text list.

ChristianEhrlicher
22nd April 2020, 17:11
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.

vitalic
22nd April 2020, 20:10
So I added a debug to the while statement, to see what is hold in vectorOfVectorsOfStrings



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



QVector(QVector("19", "sunshine live", "0x1234"), QVector("19", "dlf kultur", "0x9876")........)


Am I wrong and it already runs well, or am I right?

Added after 1 10 minutes:

Just to complete it, solution:



void MainWindow::on_pushButton_clicked()
{

QFile file(pfad);
if(!file.open(QFile::ReadOnly | QFile::Text)){
QMessageBox::warning(this,"..","keine datei gefunden");
return;
}

QTextStream in_file(&file);
QString text;

QVector<QVector<QString>> vectorOfVectorsOfStrings;
QVector<QString> zeile;

while (!in_file.atEnd()) {
text = in_file.readLine();
qDebug() << text;

QStringList split_text = text.split(",");
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]);
}
}

d_stranz
28th April 2020, 17:44
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.