struct and binary file
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
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
October 04, 2000 05:38 PM
"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
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
Popular Topics
Advertisement
Recommended Tutorials
Advertisement