Results 1 to 5 of 5

Thread: reinterpretcast help . from struct to stream and vice-versa

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default reinterpretcast help . from struct to stream and vice-versa

    I have a stream to store and retrieve values from - to an structure


    Qt Code:
    1. My_structure {
    2. int A;
    3. double B;
    4. };
    5.  
    6. put (My_structure &value) {
    7. const char * buffer ;
    8. buffer = reinterpret_cast<const char*> (&value);
    9. the_stream.write(buffer,sizeof value);
    10. }
    11.  
    12. get (My_structure & value) {
    13. char * buffer ;
    14. buffer = reinterpret_cast<char*> (&value);
    15. the_stream.get(buffer, sizeof value);
    16. value = *reinterpret_cast<My_structure*> (buffer);
    17.  
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    This compile but I get strange values.... (the tellg and tellp are well pointed to zero before starts the get's)
    I dont know how to write this last line (value = ) to get the right values...

    Any help would be appreciated.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: reinterpretcast help . from struct to stream and vice-versa

    the last line (value =), is not required, the_stream.get() will update the buffer (I assume so), as buffer is already pointing to value, value is updated indirectly (by updating buffer), unless you change buffer in side the_stream.get().

  3. #3
    Join Date
    May 2011
    Posts
    240
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: reinterpretcast help . from struct to stream and vice-versa

    One problem is on the line the_stream.get(buffer, sizeof value); -- sizeof value returns the lenght of a reference variable, not the size of the structure.

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: reinterpretcast help . from struct to stream and vice-versa

    Both should be same

  5. #5
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: reinterpretcast help . from struct to stream and vice-versa

    Thanks. Your right

Similar Threads

  1. Replies: 3
    Last Post: 13th May 2011, 13:46
  2. Replies: 10
    Last Post: 15th March 2011, 14:14
  3. Mapping JavaScript object to QT (and vice versa)
    By leoalvesmachado in forum Newbie
    Replies: 2
    Last Post: 30th June 2010, 19:00
  4. conversion between string to hex and vice versa
    By mohanakrishnan in forum Newbie
    Replies: 2
    Last Post: 5th December 2009, 11:25
  5. Convert QByteArray to object and vice versa
    By DiamonDogX in forum Qt Programming
    Replies: 4
    Last Post: 20th July 2009, 20:07

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.