Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFile - pos() not equal to sum of bytes read?

QFile - pos() not equal to sum of bytes read?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 529 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QJames
    wrote on last edited by
    #1

    Hi,
    I'm pretty new to qt (and c++ in general), so maybe this is not a qt specific "problem".
    I have a binary file that I open and I read a few bytes. After that I read a few more bytes in a loop. Subsequently, as far as I understood, file.pos() should be equal to all the bytes I have read so far, but for a few files, there is a mismatch.

    code snippet:

    [CODE]
    char buffer[255];
    qint64 bytesRead;
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
        qDebug() << "could not open" << fileName;
        return -1;
    }
    bytesRead = file.read(buffer, 16);
    if(bytesRead != 16){
       qDebug() << "mismatch bytes read" << bytesRead << " != " << 16;
    } 
    if(file.pos() != 16){
       qDebug() << "mismatch" << file.pos() << " != " << 16;
    }
    ...
    for (int i = 0; i < 7; i++) {
       bytesRead = file.read(buffer, 24);
       ...
       if(bytesRead != 24){
          qDebug() << "mismatch bytes read" << bytesRead << " != " << 24;
       } 
       if(file.pos() != (16 + (i+1)*24)){
         qDebug() << "mismatch" << file.pos() << " != " << (16 + (i+1)*24);
        }
    }
    file.close()
    

    and the output is

    mismatch  185  !=  184
    

    This happens not on all files, so I am wondering what I am doing wrong...
    Any help or pointer is appreciated!

    Pl45m4P JonBJ 2 Replies Last reply
    0
    • Q QJames

      Hi,
      I'm pretty new to qt (and c++ in general), so maybe this is not a qt specific "problem".
      I have a binary file that I open and I read a few bytes. After that I read a few more bytes in a loop. Subsequently, as far as I understood, file.pos() should be equal to all the bytes I have read so far, but for a few files, there is a mismatch.

      code snippet:

      [CODE]
      char buffer[255];
      qint64 bytesRead;
      QFile file(fileName);
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
          qDebug() << "could not open" << fileName;
          return -1;
      }
      bytesRead = file.read(buffer, 16);
      if(bytesRead != 16){
         qDebug() << "mismatch bytes read" << bytesRead << " != " << 16;
      } 
      if(file.pos() != 16){
         qDebug() << "mismatch" << file.pos() << " != " << 16;
      }
      ...
      for (int i = 0; i < 7; i++) {
         bytesRead = file.read(buffer, 24);
         ...
         if(bytesRead != 24){
            qDebug() << "mismatch bytes read" << bytesRead << " != " << 24;
         } 
         if(file.pos() != (16 + (i+1)*24)){
           qDebug() << "mismatch" << file.pos() << " != " << (16 + (i+1)*24);
          }
      }
      file.close()
      

      and the output is

      mismatch  185  !=  184
      

      This happens not on all files, so I am wondering what I am doing wrong...
      Any help or pointer is appreciated!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @QJames

      file.pos() returns the current position, not the sum of all bytes read.
      Any chance that something went wrong in these [...] lines?

      https://doc.qt.io/qt-5/qiodevice.html#pos


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      Q 1 Reply Last reply
      2
      • Q QJames

        Hi,
        I'm pretty new to qt (and c++ in general), so maybe this is not a qt specific "problem".
        I have a binary file that I open and I read a few bytes. After that I read a few more bytes in a loop. Subsequently, as far as I understood, file.pos() should be equal to all the bytes I have read so far, but for a few files, there is a mismatch.

        code snippet:

        [CODE]
        char buffer[255];
        qint64 bytesRead;
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text)){
            qDebug() << "could not open" << fileName;
            return -1;
        }
        bytesRead = file.read(buffer, 16);
        if(bytesRead != 16){
           qDebug() << "mismatch bytes read" << bytesRead << " != " << 16;
        } 
        if(file.pos() != 16){
           qDebug() << "mismatch" << file.pos() << " != " << 16;
        }
        ...
        for (int i = 0; i < 7; i++) {
           bytesRead = file.read(buffer, 24);
           ...
           if(bytesRead != 24){
              qDebug() << "mismatch bytes read" << bytesRead << " != " << 24;
           } 
           if(file.pos() != (16 + (i+1)*24)){
             qDebug() << "mismatch" << file.pos() << " != " << (16 + (i+1)*24);
            }
        }
        file.close()
        

        and the output is

        mismatch  185  !=  184
        

        This happens not on all files, so I am wondering what I am doing wrong...
        Any help or pointer is appreciated!

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @QJames
        You are opening for QIODevice::Text. I can't recall how each of pos() and read() count, you should check whether one counts e.g. \r\n as 1 while the other does not? Perhaps the difference between files is down to the "newline" recognition/counting? Certainly remove the QIODevice::Text and compare. IIRC, text read makes pos() only take on certain values/validity anyway, there's something in the docs...

        1 Reply Last reply
        4
        • Pl45m4P Pl45m4

          @QJames

          file.pos() returns the current position, not the sum of all bytes read.
          Any chance that something went wrong in these [...] lines?

          https://doc.qt.io/qt-5/qiodevice.html#pos

          Q Offline
          Q Offline
          QJames
          wrote on last edited by
          #4

          @Pl45m4 said in QFile - pos() not equal to sum of bytes read?:

          @QJames

          file.pos() returns the current position, not the sum of all bytes read.
          Any chance that something went wrong in these [...] lines?

          https://doc.qt.io/qt-5/qiodevice.html#pos

          sure, but it should be the same, shouldn't it? the position should move along with bytes written/read.

          @JonB said in QFile - pos() not equal to sum of bytes read?:

          @QJames
          You are opening for QIODevice::Text. I can't recall how each of pos() and read() count, you should check whether one counts e.g. \r\n as 1 while the other does not? Perhaps the difference between files is down to the "newline" recognition/counting? IIRC, text read makes pos() only take on certain values/validity anyway, there's something in the docs...

          thank you, that was it!!
          Removing the QIODevice::Text flag solved it.

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved