Okay so I have been trying to read in a binary file that is full of floats (little endian, single precision) and have managed to do that with QDataStream (see code below). The problem is though that these files are of the order of 531x50000 floats, and where each of the 531 represents a spectrum. If i loop over the whole file with 2 for loops (e.g. for t=0,530 and for i=0,49999) then it is really quite slow, the whole file is only 54MB. And as far as i can tell I can only read in one float at a time. Ideally I dont want to read the entire file (sometimes I might though), but I would prefer to read in an entire spectrum in one go e.g. all 531 float values at once rather than using nested loops. Can anyone point me in the right direction?
qint64 pos( 0);
float bob;
QVector<float> vect(531);
bool ok = 1;
.......
stream.
setFloatingPointPrecision(QDataStream::SinglePrecision);
stream.device()->reset();
for(int i=0;i<531;i++){
stream.device()->seek( pos );
stream >> bob;
vect[i] = bob;
pos +=4;
stream.device()->reset();
}
qint64 pos( 0);
float bob;
QVector<float> vect(531);
bool ok = 1;
.......
QFile file(fileName);
file.open(QIODevice::ReadOnly );
QDataStream stream( &file );
stream.setByteOrder(QDataStream::LittleEndian);
stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
stream.device()->reset();
for(int i=0;i<531;i++){
stream.device()->seek( pos );
stream >> bob;
vect[i] = bob;
pos +=4;
stream.device()->reset();
}
To copy to clipboard, switch view to plain text mode
I also tried the above using readRawData but couldnt get it to work at all as the toFloat function always returns FALSE. I tried this to see if I could get it to work so I could then make my datain array have a size of 4x531 and just read in a whole spectrum in one go. What did I do wrong. I am a complete noob as well.
qint64 pos( 0);
datain.resize(4);
float bob2;
bool ok = 1;
...
stream.
setFloatingPointPrecision(QDataStream::SinglePrecision);
stream.device()->reset();
for(int i=0;i<531;i++){
stream.device()->seek( pos );
stream.readRawData( datain.data(), datain.size() );
bob2 = datain.toFloat(&ok); <- THIS ALWAYS RETURNS A FALSE VALUE :(
pos +=4;
stream.device()->reset();
}
qint64 pos( 0);
QByteArray datain;
datain.resize(4);
float bob2;
bool ok = 1;
...
QFile file(fileName);
file.open(QIODevice::ReadOnly );
QDataStream stream( &file );
stream.setByteOrder(QDataStream::LittleEndian);
stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
stream.device()->reset();
for(int i=0;i<531;i++){
stream.device()->seek( pos );
stream.readRawData( datain.data(), datain.size() );
bob2 = datain.toFloat(&ok); <- THIS ALWAYS RETURNS A FALSE VALUE :(
pos +=4;
stream.device()->reset();
}
To copy to clipboard, switch view to plain text mode
I should add that I have got the seek(pos) option in there because I was playing around to see what it was reading. If I read my data in from different parts of the file then I will have to use seek.
Cheers
Oz
Bookmarks