Qt binary read error in qDatastream

I am reading the binary created by the sensor. I have a problem reading a float with varying precision (32 or 64). I can read them in MATLAB (64-bit), but Qt (32-bit on Windows) gives the wrong values. I can read before dtmth (please edit the structure below). After that, I get the Inf value for baseline . Actually this value is 0 . As you can see, I changed the MSB (LittleEndian). If I save BigEndian, I get 0 for the baseline, but other values ​​are wrong. My desktop is 64 bit.

I checked the number of bytes and they are correct. I think the problem is the accuracy of the machine.

 QDataStream in(&file); in.setByteOrder(QDataStream::LittleEndian); params p; in >> p.filetype; in >> p.projectid; in >> p.datamin; in >> p.dtyear; in >> p.dtmth; in >> p.baseline; in >> p.startfrequ; 

Where p is the structure defined as:

  struct params { quint8 filetype; quint16 projectid; double datamin; quint16 dtyear; quint8 dtmth; float baseline; double startfrequ; }; 

I can read them in MATLAB. My matlab is a 64 bit version where I read data types as follows:

 MATLAB: uint8 filetype; uint16 projectid; float64 datamin; uint16 dtyear; uint8 dtmth; float32 baseline; float64 startfrequ; 

Let me know if I missed any details.

EDIT:

Reading file:

  QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QString(), tr("Raw Files (*.msr);;All files (*.*)")); if (!fileName.isEmpty()) { qDebug("Attempting to open file.."); QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { QMessageBox::critical(this, tr("Error"), tr("Could not open file")); return; } QDataStream in(&file); 

Thank you very much in advance.

+4
source share
1 answer

What version of Qt are you using? If the version exceeds Qt 4.6, then the default accuracy is 64 bits, which means that Qt will try to read your float 32 as float 64 , you need to manually set the accuracy using in.setFloatingPointPrecision ( QDataStream::SinglePrecision);

  in >> p.filetype; in >> p.projectid; in >> p.datamin; in >> p.dtyear; in >> p.dtmth; in.setFloatingPointPrecision(QDataStream::SinglePrecision); in >> p.baseline; in.setFloatingPointPrecision(QDataStream::DoublePrecision); in >> p.startfrequ; 

From your comment, it seems like this was a problem. Indeed, if you set it to the same precision and try to read p.datamin or p.startfrequ (64 bits), then the data stream will read them as 32-bit floats. and not only p.datamin will be incorrect, but also all values ​​after it.

First, check that my sentence works after the last line

  if(in.status() == QDataStream::ReadCorruptData){ qDebug() << "still doesnt work"; } 
+3
source

All Articles