QDataStream. How to skip data using serialization?

  • 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);
    qDebug() << "container"<< _containerPath << " error=" << container.errorString();

    // 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";
    //check for correct version of the storage file
    qint32 version;
    stream >> version;
    if (version < Version )
        qDebug() << "file version is too old";
    else if (version > Version )
        qDebug() << "file version is too new";
    // 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;


  • 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.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.