Jump to content
  • Advertisement
Sign in to follow this  
tcsavage

fwrite an object to file, strange problem

This topic is 3415 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been using fwrite to dump some structures to a file for later retrieval ad so far it has worked great, but now I have found a problem. I am trying to dump an object called "scene_object_store" of type "StoreSceneObject" which contains (among other things) four std::string objects: name, path, materialName and materialPath. All of the variables are written correctly and can be retrieved, except for the third std::string, materialName. In the debugger, I can see that scene_object_store contains all of the correct values but the data I retrieve from the file just has junk in materialName. The materialPath string works as I would expect it to and it is created, set and used in all the same places that materialName is so nothing is different between them. Has any one else experienced this or have any clues as to what I can do to fix it?

Share this post


Link to post
Share on other sites
Advertisement
Are you writing (char*)&scene_store_object? That doesn't work for non-POD types.

MVC's std::string implementation does something like this:
class std::basic_string {
buffer[16];
char * heap_buffer;
};


If the text is less than 16 characters, then it will be written correctly simply because it is allocated inside buffer. If it's longer, it's allocated on heap, so buffer contains garbage, and actual data is in heap_buffer.

Non-POD types need to be serialized explicitly. Handle each member individually (.size() and .c_str()).

Share this post


Link to post
Share on other sites
Thanks for pointing that out Antheus, the string in question was longer than 16 characters. I'll take a look at SiCrane's link now.

Quick question, what do you mean by "non-POD"?

Share this post


Link to post
Share on other sites
Quote:
Original post by tcsavage
Thanks for pointing that out Antheus, the string in question was longer than 16 characters. I'll take a look at SiCrane's link now.

Quick question, what do you mean by "non-POD"?


POD stands for Plain Old Data, and like many acronyms can be found on Google.

[Edited by - rip-off on May 17, 2009 10:50:41 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
That link is a lot less funny if you screw up the search text.


Arg, just dropped a " character in the link [sad]

The other text wass actually HTML from my signature I think.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
POD C++2? What's the 2 for?

That was my typo (I have corrected it in the mean time). I must not have hit shift when typing the quote. That is where the " character is on keyboards here (think it may differ on American keyboard layouts). Cheers for not making a big deal out of it or anything [grin]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!