having trouble reading from textstream
-
Greetings..
I am having some problem reading from the text stream
in the below code first i am reading the whole content of a file to a string and check if the pressed item is present if it is present then i will proceed furtherif i remove int pos = str.indexOf(item->text()); and datafile.seek(pos);
these two lines it will not enter the while loop
why soi dont want to seek to any position i want to read from the beginning only but it is entering to the while loop only if i add above mentioned lines
void MainWindow::on_listWidget_itemPressed(QListWidgetItem *item) { QFile datafile("/home/msg.txt"); if(!datafile.open(QIODevice::ReadOnly)) { qDebug() << "file not opened"; } QString line; QTextStream in(&datafile); QString str = in.readAll(); if (str.contains(item->text())) { int pos = str.indexOf(item->text()); datafile.seek(pos); while (in.readLineInto(&line)) { //skipping commented line if (line[0] =='#') { continue; } QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts); qDebug()<<list; if (list[0] == item->text()) { msgname = list[0]; } if( list[0] == "MSG_ID") { msgid = list[1].toInt(); qDebug()<< msgid; } ui->textEdit->setText(QString("Messagename = %1").arg(msgname)); ui->textEdit->append(QString("MessageId = %1").arg(msgid)); } } }Thanks in advance
-
Greetings..
I am having some problem reading from the text stream
in the below code first i am reading the whole content of a file to a string and check if the pressed item is present if it is present then i will proceed furtherif i remove int pos = str.indexOf(item->text()); and datafile.seek(pos);
these two lines it will not enter the while loop
why soi dont want to seek to any position i want to read from the beginning only but it is entering to the while loop only if i add above mentioned lines
void MainWindow::on_listWidget_itemPressed(QListWidgetItem *item) { QFile datafile("/home/msg.txt"); if(!datafile.open(QIODevice::ReadOnly)) { qDebug() << "file not opened"; } QString line; QTextStream in(&datafile); QString str = in.readAll(); if (str.contains(item->text())) { int pos = str.indexOf(item->text()); datafile.seek(pos); while (in.readLineInto(&line)) { //skipping commented line if (line[0] =='#') { continue; } QStringList list = line.split(QLatin1Char(':'), QString::SkipEmptyParts); qDebug()<<list; if (list[0] == item->text()) { msgname = list[0]; } if( list[0] == "MSG_ID") { msgid = list[1].toInt(); qDebug()<< msgid; } ui->textEdit->setText(QString("Messagename = %1").arg(msgname)); ui->textEdit->append(QString("MessageId = %1").arg(msgid)); } } }Thanks in advance
@kook said in having trouble reading from textstream:
QString str = in.readAll(); ... while (in.readLineInto(&line))If you have already done a
readAll()from the stream, where do you expect a subsequentreadLineInto()to start reading from? Either do your work on thestrcontaining all the read data or don't do that and do thereadLineInto(), not both. Or if you do want to use both as you have shown, it is not surprising you have to godatafile.seek(pos);to reposition the reader; but I would either do everything off thestror everything off thein.