Read a file with text header and binary data

  • In C++ if I wanted to read a file with a text header and binary data, I would just do something like this:

    string code;
    int width, height, maxval;
    myStream >> code >> width >> height >> maxval;
    myStream.get() //skip whitespace, width*height*depth);

    I can't seem to figure out how to do this with a QDataStream and a device. Now, I know that I could do this like you would in C by looking for whitespace, etc., but I was hoping to avoid that mess. What I have tried so far is:

        QDataStream in(device);
        QString sCode, sWidth,sHeight,sMax;
        in >> sCode >> sWidth >> sHeight >> sMax;
        width = sWidth.toInt();
        height = sHeight.toInt();

    but it doesn't seem to want to read the strings. The documentation for QString says:

    QDataStream &operator>>(QDataStream &stream, QString &string)
    Reads a string from the specified stream into the given string.

    So it would seem to me that should work? I am just trying to figure out what I am misinterpreting or not understanding.


  • Qt Champions 2017

    When you say string, you do mean a QString ?
    As it might not consider it a string if
    you didnt stream it as << Qstring("my textheader")

  • You can direct save as format QTextDocument... or the class in your use for this binary data...
    color images border margin always remain perfect no lost data...
    search google file "Image_Page_Struct_Mime.h" as sample ...
    You make your own mimetype file...

    class SPics
        enum { MAGICNUMBER = 0xFFAAFFAA, VERSION = 1 };
        SPics& operator=( const SPics& d );
        bool operator!=( const SPics& d );
        operator QVariant() const
            return QVariant::fromValue(*this);
        QPixmap erno_pix();
        QString web();
        QPixmap pix();
        QString FileName();
        void SavePixThread( QString dir = QString() );
        void set_pics( const QPixmap * barcode );
        void set_pics( QPixmap barcode );
        inline int picskilo()
            return data.size();
        QByteArray streams()
          return qUncompress( data ); 
        /* set src url attributes and write file to session !!!! */
        bool FopSaveImage( QDomElement e );
        QUrl indoc();
        /* vars permanent to put on stream  */
        QString name;
        QString info;
        QByteArray extension;
        QByteArray data;  /* qCompress */
    inline QDebug operator<<(QDebug debug, SPics& udoc)
        debug.nospace() << "SPics(name."
        << << ","
        << << ",size()"
        << << ",type()"
        << udoc.extension << ")";

  • Ok - It took a littlw while but I figured it out. I just need to think more in terms of Qt, and less std C++. Anyway, the following works for what I need to do (whic is just a small proof of concept project, so it isn't elegant or probably even effiicent)

        QStringList temp;
        QDataStream in(device);
        QByteArray array = device->readLine();
        code = QString(array).trimmed();
        array = device->readLine();
        if(array[0] == '#')
            array = device->readLine();
        temp = QString(array).trimmed().split(' ');
        width = temp[0].toInt();
        height = temp[1].toInt();
        size = width*height*3;
        maxval = QString(array).trimmed().toInt();

Log in to reply

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