Results 1 to 4 of 4

Thread: Mapping a file to a struct

  1. #1
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Mapping a file to a struct

    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!

  2. #2
    Join Date
    Mar 2007
    Posts
    57
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mapping a file to a struct

    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

  3. #3
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Mapping a file to a struct

    I'd do something like this:

    Qt Code:
    1. struct MyStruct {
    2. // ...;
    3. };
    4.  
    5. QFile file;
    6. // ...
    7. 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.

  4. #4
    Join Date
    Apr 2009
    Posts
    63
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Mapping a file to a struct

    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.

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  2. Replies: 16
    Last Post: 23rd May 2008, 10:12
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  4. Make error with Qt Jambi 4.3.2_01
    By pamalite in forum Installation and Deployment
    Replies: 0
    Last Post: 22nd November 2007, 12:05
  5. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.