QDataStream read a structure



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



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


Log in to reply
 

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