Search code examples
qtfileqtextstream

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)


Solution

  • 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());