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. read binary data from QDataStream with feedback
Qt 6.11 is out! See what's new in the release blog

read binary data from QDataStream with feedback

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by DungeonLords
    #1

    There is official example how to read binary data from a stream (from file). I want the same but with feedback: how to know that there was error while deserializing?

    This is my example

    void MainClass::read(){
        my_struct_t obj;
        QFile file("file.dat");
        file.open(QIODevice::ReadOnly);
        QDataStream in(&file); // read the data serialized from the file
        in.startTransaction();
        in >> obj.a; // extract 3 and true
        if(in.commitTransaction()){
            qDebug().noquote().nospace() << "read is ok";
        }else{
            qDebug().noquote().nospace() << "unknown error";
        }
        qDebug().noquote().nospace() << "obj.a=" << obj.a;
    }
    

    To know about deserializing errors, should I use in.commitTransaction() and in.commitTransaction()? Or it is for special interfaces like serial port/socket only?

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What other error do you expect than there was not enough data available for deserialization?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      D 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        What other error do you expect than there was not enough data available for deserialization?

        D Offline
        D Offline
        DungeonLords
        wrote on last edited by DungeonLords
        #3

        @Christian-Ehrlicher said in read binary data from QDataStream with feedback:

        What other error do you expect than there was not enough data available for deserialization?

        I expect the only one type of error: if(data available % data of pack) {error();}. In human speech: not enough data in file or too much data in file is error. But how to now it happen?

        I was thinking to check by

        if(!(file.size() % __datasizeof(my_struct_t))) makeWarning();
        

        But unfortunately I use GCC last version, but __datasizeof() is still unavailable... Am I right?

        Well, I can calc struct size manually and check by

        if(!(file.size() % magicConstant)) makeWarning();
        

        But I want to avoid magicConstant..

        Christian EhrlicherC 1 Reply Last reply
        0
        • D DungeonLords

          @Christian-Ehrlicher said in read binary data from QDataStream with feedback:

          What other error do you expect than there was not enough data available for deserialization?

          I expect the only one type of error: if(data available % data of pack) {error();}. In human speech: not enough data in file or too much data in file is error. But how to now it happen?

          I was thinking to check by

          if(!(file.size() % __datasizeof(my_struct_t))) makeWarning();
          

          But unfortunately I use GCC last version, but __datasizeof() is still unavailable... Am I right?

          Well, I can calc struct size manually and check by

          if(!(file.size() % magicConstant)) makeWarning();
          

          But I want to avoid magicConstant..

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          @DungeonLords said in read binary data from QDataStream with feedback:

          But how to now it happen?

          QDS::commitTransaction() will return false and QDataStream::status() will tell you why.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • D Offline
            D Offline
            DungeonLords
            wrote on last edited by
            #5

            Thanks. Can I drop startTransaction() and commitTransaction()? Use like this

            void MainClass::read(){
                my_struct_t obj;
                QFile file("file.dat");
                file.open(QIODevice::ReadOnly);
                QDataStream in(&file); // read the data serialized from the file
                in >> obj.a;
                if(in.status() == QDataStream::Ok){
                    qDebug().noquote().nospace() << "read is ok";
                }else{
                    qDebug().noquote().nospace() << "error code " << in.status();
                }
                qDebug().noquote().nospace() << "obj.a=" << obj.a;
            }
            
            JonBJ 1 Reply Last reply
            0
            • D DungeonLords

              Thanks. Can I drop startTransaction() and commitTransaction()? Use like this

              void MainClass::read(){
                  my_struct_t obj;
                  QFile file("file.dat");
                  file.open(QIODevice::ReadOnly);
                  QDataStream in(&file); // read the data serialized from the file
                  in >> obj.a;
                  if(in.status() == QDataStream::Ok){
                      qDebug().noquote().nospace() << "read is ok";
                  }else{
                      qDebug().noquote().nospace() << "error code " << in.status();
                  }
                  qDebug().noquote().nospace() << "obj.a=" << obj.a;
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @DungeonLords
              Yes, because you reading from regular file. You would need it if you were reading from a streaming device instead.

              Using Read Transactions

              When a data stream operates on an asynchronous device, the chunks of data can arrive at arbitrary points in time. The QDataStream class implements a transaction mechanism that provides the ability to read the data atomically with a series of stream operators

              1 Reply Last reply
              0

              • Login

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