Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-80482

QTextStream::atEnd always returns false (when reading from stdlib FILE (in text mode))

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P3: Somewhat important P3: Somewhat important
    • None
    • 5.14
    • Core: I/O
    • None
    • Windows

      The following code will end up into an endless while loop:

      #include <QtCore>
      
      int main(int argc, char *argv[])
      {
          const char fileWithCRLF[] = "c:\\temp\\fileWithCRLF.txt";
      
          {   // Generating text file
              QFile textFile(fileWithCRLF);
              textFile.open(QIODevice::WriteOnly);
              textFile.write("Qt\u000d\u000a\0"); // "Qt<CR+LF>"
          }
      
          FILE *stdFile = fopen(fileWithCRLF, "r"); // Implicitly opens in text mode, filters out CR
          QTextStream is(stdFile);
          while (!is.atEnd()) // atEnd() eternally returns false
              puts(qPrintable(is.readLine()));
      
          return 0;
      }
      

      A FILE handle to a text file containing CR+LF newlines is opened. The implicit open mode is by default "t". That text mode causes CR (0x0d) bytes of CR+LF pairs to be filtered out. Therefore, out of the originally 4 bytes in the file, only 3 are read and later processed by QTextStream.
      QTextStream::atEnd() returns false if the current stream position in the buffer (3 bytes) is smaller than the size of the original file from the file system (4 bytes).
      The comparison of buffer position vs. file size happens in QIODevice::bytesAvailable().

      A real life case of this admittetly unusual scenario is in qmake's sed replacement: https://code.woboq.org/qt5/qtbase/qmake/main.cpp.html#153

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

            thiago Thiago Macieira
            portale Alessandro Portale
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:

                There are no open Gerrit changes