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 a file with text header and binary data
Forum Update on Monday, May 27th 2025

Read a file with text header and binary data

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.7k 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.
  • T Offline
    T Offline
    tenspd137
    wrote on 30 Sept 2017, 03:42 last edited by tenspd137
    #1

    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
    myStream.read(somebuffer, 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.

    Thanks!

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tenspd137
      wrote on 2 Oct 2017, 16:15 last edited by
      #4

      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;
          array=device->readLine();
          maxval = QString(array).trimmed().toInt();
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 30 Sept 2017, 09:40 last edited by mrjj
        #2

        Hi
        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")

        1 Reply Last reply
        1
        • P Offline
          P Offline
          patrik08
          wrote on 30 Sept 2017, 13:55 last edited by
          #3

          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
          {
          public:
              enum { MAGICNUMBER = 0xFFAAFFAA, VERSION = 1 };
              SPics();
              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 */
          };
          
          Q_DECLARE_METATYPE(SPics);
          
          inline QDebug operator<<(QDebug debug, SPics& udoc)
          {
              debug.nospace() << "SPics(name."
              << udoc.name << ","
              << udoc.info << ",size()"
              << udoc.data.size() << ",type()"
              << udoc.extension << ")";
              return debug.space();
          }
          
          1 Reply Last reply
          0
          • T Offline
            T Offline
            tenspd137
            wrote on 2 Oct 2017, 16:15 last edited by
            #4

            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;
                array=device->readLine();
                maxval = QString(array).trimmed().toInt();
            
            1 Reply Last reply
            0

            1/4

            30 Sept 2017, 03:42

            • 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