I am aware of the QFile::map() function, which maps the bytes of a file into memory. I am wondering how to map the bytes of a file to a struct I have defined, being able to read/write to the file using the struct (in memory). Thanks!
I am aware of the QFile::map() function, which maps the bytes of a file into memory. I am wondering how to map the bytes of a file to a struct I have defined, being able to read/write to the file using the struct (in memory). Thanks!
Sounds like you want a union. Or well, maybe you don't *want* a union, but at least it sound like that's wat you want to do. Or just typecast your memory to the struct?
Or perhaps explain the problem better
I'd do something like this:
Qt Code:
struct MyStruct { // ...; }; QFile file; // ... MyStruct* myStruct = reinterpret_cast<MyStruct*>(file.map(offset, sizeof(MyStruct)));To copy to clipboard, switch view to plain text mode
But to access binary data in a file, I'd rather use QDataStream.
Actually, what I'm currently using is QDataStream and seeking to certain offsets in the file that correspond to offsets of data members in my struct (by using the offsetof macro). Why do you say using QDataStream would be better in this case? Thanks.
Bookmarks