Advertisement

struct and binary file

Started by October 04, 2000 02:04 PM
1 comment, last by Mansion 24 years, 3 months ago
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
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
Advertisement
"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

This topic is closed to new replies.

Advertisement