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. QDataStream. How to skip data using serialization?
Forum Updated to NodeBB v4.3 + New Features

QDataStream. How to skip data using serialization?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.4k Views 1 Watching
  • 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.
  • _ Offline
    _ Offline
    _Dima
    wrote on last edited by
    #1

    Could somebody advise me how to skip data using serialized stream?

    There is method "skip", but it needs just amount of raw bytes. I would like to use just Qt serialization mechanism.

    Some details:

    1. What am I trying to do?
      I want to implement small file container which encapsulates few files inside.
      !http://s22.postimg.org/j8g8roukx/FAT_schema.png(file container)!

    2. What is problem?
      That's reading of the certain file by index. For example, skip QByteArray's (from 0 to index-1) and read just QByteArray at "index" position.

    3. Some proof code
      @#include <QFile>
      #include <QDataStream>

    static const int QDataStreamVersion = QDataStream::Qt_4_8;
    static const quint32 Magic = 0xA0B0C0D0;
    static const quint32 Version = 1;

    struct FatItem
    {
    QString fname;
    quint32 index;
    };

    QDataStream & operator>> (QDataStream & _stream, FatItem & _fatItem)
    {
    _stream >> _fatItem.fname;
    _stream >> _fatItem.index;
    return _stream;
    }

    void openContainerAndExtractAllFiles(QString _containerPath, QString _destinationDir)
    {
    //open storage file
    QFile container(_containerPath);
    if(!container.open(QIODevice::ReadOnly))
    {
    qDebug() << "container"<< _containerPath << " error=" << container.errorString();
    return;
    }

    // assign stream to file
    QDataStream stream(&container);
    
    //check for key number
    quint32 magic;
    stream &gt;&gt; magic;
    if (magic != Magic)
    {
        qDebug() &lt;&lt; "Storage file: bad format";
        return;
    }
    
    //check for correct version of the storage file
    qint32 version;
    stream >> version;
    if (version < Version )
    {
        qDebug() << "file version is too old";
        return;
    }
    else if (version > Version )
    {
        qDebug() << "file version is too new";
        return;
    }
    
    
    // load FAT
    QList<FatItem> fat;
    stream >> fat;
    
    // extract all files and put them to destination dir
    for (quint32 i=0; i<fat.count() && !stream.atEnd(); i++)
    {
        // extract
        QByteArray fData;
        stream >> fData;
    
        // put
        QFile f(_destinationDir + fat.at(i).fname);
        if (!f.open(QIODevice::WriteOnly))continue;
        f.write(fData);
        f.close();
    }
    
    container.close();
    

    }@

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      As the file layout is documented, you can find how the byte array is stored. I think it is basically just a field with the length, and then the raw data. You can read the length field yourself, and then you know how many bytes to skip to the next item.

      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