QTextStream atEnd () returns true when reading from file starts

I want to read and parse the contents of the / proc / PID / status file on a linux machine, but QTextStream.atEnd always returns true when reading starts. Code:

QString procDirectory = "/proc/";
    procDirectory.append(QString::number(PID));
    procDirectory.append("/status");
    QFile inputFile(procDirectory);
    if (inputFile.open(QIODevice::ReadOnly))
    {
        QTextStream in(&inputFile);
        QString line;

        while (!in.atEnd())
        {
            line = in.readLine();

      

The file exists and if I read the lines manually without a while statement, the files are read fine.

Am I missing something obvious?

(Debian 8 x64, QT 5.4.1 x64, gcc 4.9.2)

+3


source to share


2 answers


The preferred way to iterate over these streams is with a do / while loop. This means that the stream correctly detects Unicode before any requests are made (e.g. atEnd).



QTextStream stream(stdin);
QString line;
do {
    line = stream.readLine();
} while (!line.isNull());

      

+1


source


Nevermind found out that I need to read one line before the while clause, now it works.



+1


source







All Articles