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 read a structure

QDataStream read a structure

Scheduled Pinned Locked Moved General and Desktop
qdatastreambig endianreadbytes
2 Posts 2 Posters 2.2k 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
    dridk2
    wrote on 4 May 2015, 21:55 last edited by
    #1

    Hello,
    I m trying to read a structure from a file writted as BigEndian using QDataStream.
    For simple value this is working :

    qint16 value;
    QDataStream stream (&file);
    stream.setByteOrder(QDataStream::BigEndian);
    stream >> value; 
    qDebug()<<value;   // Return -3
    

    But if I did :

    qint16 value;
    QDataStream stream (&file);
    stream.setByteOrder(QDataStream::BigEndian);
    stream.read((char*)&value, sizeof(qint16))
    qDebug()<<value;  // -513 
    // I have to do : 
    value = qFromBigEndian(value)
    qDebug()<<value; // -3
    

    So, I want to read a struct. I cannot use ">>" operator. Then I have to use QDataStream::read. Then I must create a swap method for my structure . But it's boring and not nice..

    myStruct swap(myStruct s ) 
    {
      myStruct d ;
      d.value =  qFromBigEndian(s.value);
      d.age =  qFromBigEndian(s.age);
      return d;
    }
    

    Do you have some alternative ?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on 4 May 2015, 23:24 last edited by alex_malyu 5 Apr 2015, 23:25
      #2

      I am not sure I completely understand the problem since
      I do not see any structure in your read/write code.

      But normal way to deal with structures (the same as with classes)
      is to write/read every member separate in their natural form with provided by QDataStream functionality.
      And you certainly need to know type of the data written to the file to read it properly.
      Your problem (as far as I can understand) is related to using wrong function to read integer data,
      cause you read it as char data,

      1 Reply Last reply
      0

      2/2

      4 May 2015, 23:24

      • Login

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