QFile read only lines containing specific search term
-
Hello!
When i open a file to read all i want to select specific rows "to be saved" as an output to QStringList.
So when i read through a file and a line has ex. "orange" i want to save that line to a list.My read file is a CSV and i have my search terms in a combo box.
-
Hello!
When i open a file to read all i want to select specific rows "to be saved" as an output to QStringList.
So when i read through a file and a line has ex. "orange" i want to save that line to a list.My read file is a CSV and i have my search terms in a combo box.
@RainyDays said in QFile read only lines containing specific search term:
So when i read through a file and a line has ex. "orange" i want to save that line to a list.
And what's the exact problem you face? Read the line, check for your condition and add the values to your stringlist.
-
Hi,
While reading, no need to load useless data.
-
Ok, so here is what i came up with :)
if (database.open(QFile::ReadOnly | QIODevice::Text)) { while(!fileOutputStream.atEnd()) { lineData = fileOutputStream.readLine(); if (lineData.contains(condition)) { fileOutput.append(lineData.split("\n")); } } database.close(); } -
I assume the thing named
fileOutputis your desired QStringList.
What is the relationship between your input data, presumablydatabase, and the text stream,fileOutputStreampresumably a QTextStream, you are attempting to read from.BTW, the string returned by QTextStream::readLine() does not contain any end-of-line markers, so there is not need to try to remove them.
-
Hello!
Yes you are correct.
the setup is like thisQFile database("/RDS.csv"); QTextStream fileOutputStream(&database); QString lineData; QStringList fileOutput;And my search condition is
QString condition = ui->comboBox->currentText();Also i don't need to hunt the end of line markers.
I had this setup when i was reading all data before.
I wanted to take the first column if the row contained a search term.
But for this it works fine withfileOutput.append(lineData);