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 - read bytes, x at a time, x +offset?
QtWS25 Last Chance

QFile - read bytes, x at a time, x +offset?

Scheduled Pinned Locked Moved Solved General and Desktop
qfileqbytearrayqiodevice
4 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
    Dariusz
    wrote on 25 Apr 2019, 15:26 last edited by
    #1

    Hey

    I'm trying to read part of the file to decide if I wish to read all of the file... can any1 help me out fill in the blank?

        QByteArray someData;
        QByteArray da;
        QDataStream s(&da, QIODevice::WriteOnly);
        s << (int) 1412 << (quint16) 123143 << (bool) true << (int) someData.size() << someData;
        QFile f(path);
        if (f.open(QIODevice::ReadOnly)) {
            int initial = 0;
            quint16 qinitial = 0;
            bool good = false;
            int dataSize = 0;
            QByteArray inData = f.read(sizeof(int) + sizeof(quint16) + sizeof(bool) + sizeof(int));/// Is this correct?
    
            QDataStream in(&inData,QIODevice::ReadOnly);
            in>>initial>>qinitial>>good>>dataSize;
            if(good){
                /// Read f.read(dataSize) ??? How to read rest?
            }
        }
    

    TIA

    K J 2 Replies Last reply 25 Apr 2019, 15:55
    0
    • D Dariusz
      25 Apr 2019, 15:26

      Hey

      I'm trying to read part of the file to decide if I wish to read all of the file... can any1 help me out fill in the blank?

          QByteArray someData;
          QByteArray da;
          QDataStream s(&da, QIODevice::WriteOnly);
          s << (int) 1412 << (quint16) 123143 << (bool) true << (int) someData.size() << someData;
          QFile f(path);
          if (f.open(QIODevice::ReadOnly)) {
              int initial = 0;
              quint16 qinitial = 0;
              bool good = false;
              int dataSize = 0;
              QByteArray inData = f.read(sizeof(int) + sizeof(quint16) + sizeof(bool) + sizeof(int));/// Is this correct?
      
              QDataStream in(&inData,QIODevice::ReadOnly);
              in>>initial>>qinitial>>good>>dataSize;
              if(good){
                  /// Read f.read(dataSize) ??? How to read rest?
              }
          }
      

      TIA

      K Offline
      K Offline
      KroMignon
      wrote on 25 Apr 2019, 15:55 last edited by KroMignon
      #2

      @Dariusz Why you not simply use a QDataStream from file?

          QByteArray someData;
          QByteArray da;
          QDataStream s(&da, QIODevice::WriteOnly);
          s << (int) 1412 << (quint16) 123143 << (bool) true << (int) someData.size() << someData;
          QFile f(path);
          if (f.open(QIODevice::ReadOnly)) {
              QDataStream inData(&f);
              int initial;
              quint16 qinitial;
              bool good;
              int dataSize;
      
              inData>>initial>>qinitial>>good>>dataSize;
              if(good){
                  QByteArray buffer;
                  buffer.resize(dataSize);
                  inData.readRawData(buffer.data(), dataSize);
              }
          }
      

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      D 1 Reply Last reply 25 Apr 2019, 17:01
      3
      • K KroMignon
        25 Apr 2019, 15:55

        @Dariusz Why you not simply use a QDataStream from file?

            QByteArray someData;
            QByteArray da;
            QDataStream s(&da, QIODevice::WriteOnly);
            s << (int) 1412 << (quint16) 123143 << (bool) true << (int) someData.size() << someData;
            QFile f(path);
            if (f.open(QIODevice::ReadOnly)) {
                QDataStream inData(&f);
                int initial;
                quint16 qinitial;
                bool good;
                int dataSize;
        
                inData>>initial>>qinitial>>good>>dataSize;
                if(good){
                    QByteArray buffer;
                    buffer.resize(dataSize);
                    inData.readRawData(buffer.data(), dataSize);
                }
            }
        
        D Offline
        D Offline
        Dariusz
        wrote on 25 Apr 2019, 17:01 last edited by Dariusz
        #3

        @KroMignon looks interesting, but I want to read from xx byte to xx byte. Not all of it.

        Also ohh hmmmm that looks interesting, did not know I can do it. So if I do dataStream, hell only read specific bytes automaticly? Damn thats neat o.o / still what about reading just xx to xx ?

        Hmm I suppose I could just use skiPrawData(int) < I guess its skipRawData(sizeof(int)) or just the size I stored early.

        1 Reply Last reply
        0
        • D Dariusz
          25 Apr 2019, 15:26

          Hey

          I'm trying to read part of the file to decide if I wish to read all of the file... can any1 help me out fill in the blank?

              QByteArray someData;
              QByteArray da;
              QDataStream s(&da, QIODevice::WriteOnly);
              s << (int) 1412 << (quint16) 123143 << (bool) true << (int) someData.size() << someData;
              QFile f(path);
              if (f.open(QIODevice::ReadOnly)) {
                  int initial = 0;
                  quint16 qinitial = 0;
                  bool good = false;
                  int dataSize = 0;
                  QByteArray inData = f.read(sizeof(int) + sizeof(quint16) + sizeof(bool) + sizeof(int));/// Is this correct?
          
                  QDataStream in(&inData,QIODevice::ReadOnly);
                  in>>initial>>qinitial>>good>>dataSize;
                  if(good){
                      /// Read f.read(dataSize) ??? How to read rest?
                  }
              }
          

          TIA

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 26 Apr 2019, 05:07 last edited by
          #4

          @Dariusz Simply seek to the position where you want to read and then read the amount of bytes you need.
          https://doc.qt.io/qt-5/qfiledevice.html#seek
          https://doc.qt.io/qt-5/qiodevice.html#read-1

          You can seek to the beginning of the file if you decide to read whole file.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4

          1/4

          25 Apr 2019, 15:26

          • Login

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