Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Mansion

struct and binary file

This topic is 6498 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

hello how do i write like typedef tagplayer{ int x, y; }player; to a binary file and how to read it, with fprintf it doesnt go smooth... thank you

Share this post


Link to post
Share on other sites
Advertisement
fwrite(&player, sizeof(tagplayer),1,fp);

or you''d be better served doing this because of struct data padding.

fwrite(&player.x, sizeof(int),1,fp);
fwrite(&player.y, sizeof(int),1,fp);



My Homepage

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
"better" is in the eye of the beholder. tagplayer is unlikely to have any padding so the effort to eliminate it is wasted.

Even if padding was there, your first example (single fwrite) has less code and is arguably easier to understand. Also, copying whole structures with a single memcpy can be faster than copying every member individually even taking the padding into account.

-Mike

Share this post


Link to post
Share on other sites

  • 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!