user2195430
user2195430

Reputation: 105

QTextStream atEnd() is returning true when starting to read from a file

I want to read and parse contents of the /proc/PID/status file on a linux machine, but the QTextStream.atEnd is always returning true when starting to read. The 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();

File exists and if I read lines manually without the while expression, the files are read normally.

Did I miss something obvious?

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

Upvotes: 4

Views: 1786

Answers (2)

dom0
dom0

Reputation: 7486

The preferred way oft looping over these streams is with a do/while loop. This is for allowing the stream to detect Unicode correctly before any queries (like atEnd) are made.

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

Upvotes: 1

user2195430
user2195430

Reputation: 105

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

Upvotes: 2

Related Questions