I want to read multiple float values from the file in which I already wrote using this:
QDataStream &operator<<(QDataStream &out, const SomeClass &obj)
{
out << obj.maxX << obj.maxY << obj.minX << obj.minY;
out << (int) obj.points.size();
for(int c = 0; c < obj.points.size(); ++c){
out << obj.points.at(c).floatX << obj.points.at(c).floatY;
}
return out;
}
And I can read this file using this:
QDataStream &operator>>(QDataStream &in, SomeClass &obj)
{
in >> obj.maxX >> obj.maxY >> obj.minX >> obj.minY;
int pointsSize = 0;
in >> pointsSize;
for(int c = 0; c < pointsSize; ++c){
float x = 0, y = 0;
in >> x >> y;
obj.points.push_back(Point(x, y));
}
return in;
}
But it's not so efficient to read these floats separately, so I want to read first 4 floats(maxX, maxY, minX, minY) together, then read one int, and then read all other floats(instances of floatX and floatY) together.
I already tried this:
QDataStream in(&file);
QByteArray ba;
in.readRawData(ba.data(), 4*sizeof(float));
float array[4];
memcpy(&array, ba.constData(), ba.size());
But it gives wrong result.
So, how can I read many floats in one buffer array or vector?
UPD:
I already saw getline() too, but how exactly can I iterate over char *
from binary file and get floats from it?
QDataStream
operators, you can be sure that this gets taken care of. So I wouldn't do the reading of raw bytes if it can be avoided, and prefer the official functions. – Karsten Koop