PDA

View Full Version : accessing data in a QFile



nass
21st September 2006, 14:19
hello everyone,
i have been ssaving some variable values in a file using QFile and QDataStream.
the variables are of types Q_UINT8, Q_INT8, Q_UINT32, Q_INT32, float, double and even QString...
write and read operation work wonderfully so far..

but now i need to be able to access certain variables down the stream..
so if i have written the stream in the file as:

QDataStream ds(&aQFile)
ds<<aUINT8<<aUINT32<<aINT8<<aFloat<<aDouble<<aQString<<anotherINT8..... etcetc

and now i want to access directly aINT8, how do i do it?
i read in te documentation at the QFile::at(Offset off) function... i guess the offset is just an integer number of the counted bytes till the beginning of the variable i want to read..
however i wonder how im gonna compute the offsets..
mean in the above case its easy to find the offset of aINT8.. it is 5 (bytes) ... or not?
however how can i get the offset of anotherINT8 ?
thank you for your help
nass

high_flyer
21st September 2006, 17:25
how can i get the offset of anotherINT8 ?
You have to know your offeset when accesing it, not way around it.
(so in the example you gave you have to know how many baytes are written before 'anotherINT8')
But usually, you would use structures in order to serialize data, and not write just like that in to a file.
So for exmple, you could create a struct or a class called DataBlock, in which each field would have a name and type.
Like so:
typedef struct DataBlock {
Q_UINT32 blockNum;
Q_UINT8 feild1;
QString feild2;
float feild3;
}
You then can overload the global QDataStream & operator<<() and QDataStream & operator>>() to make reading and writing data blocks easy.
All you need then is to know in which block the feild you are looking for is at, read that much blocks, and expose the field.