PDA

View Full Version : reading from file



Shien
22nd January 2011, 18:20
So, I am trying to read form .txt file. While I am doing it I try to search for match of lineEdit value.

QFile file(":/data/litEng.txt");
file.open(QFile::ReadOnly);
if (!file.isOpen())
{
qDebug() << "error";

}
QTextStream stream ( &file );
QString line;
while( !stream.atEnd())
{
line = stream.readLine();
if(line == wordEdit->text())
{
qDebug() << "ok";
}
qDebug() << "bad";
}
file.close();

but it says bad. Maybe I shouldn't use String for reading?

tbscope
22nd January 2011, 18:55
It only means that line is never equal to wordEdit->text()

Be careful with extra characters being read from the file. Spaces for example.

stampede
22nd January 2011, 19:09
if(line == wordEdit->text())
{
qDebug() << "ok";
}
qDebug() << "bad";
Maybe better would be:

f(line == wordEdit->text()){
qDebug() << "ok";
} else{
qDebug() << "bad: " << line << ";" << wordEdit->text() << ";";
}
This way you know why its "bad" (and dont print "bad" after its "ok" ;) ).