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