Mail Archives: djgpp/1997/07/14/11:48:23
On Sat, 12 Jul 1997, Deltaman wrote:
> I have a program, sort of an editor. Its data structures is like this:
>
> void save_board (map *b, char *fname)
> {
> FILE *f;
>
> f = fopen (fname, "wb");
>
> fwrite (b->data, sizeof (my_struct), mapx * mapy, f);
> fclose (f);
> }
>
> void read_board (map *b, char *fname)
> {
> FILE *f;
>
> f = fopen (fname, "r");
>
> fwrite (b->data, sizeof (my_struct), mapx * mapy, f);
> fclose (f);
> }
Unless the fopen() in read_board() is a typo, you are opening the file
for write in binary mode then opening it for read in text mode. A text
mode read is going to eat any null character and anything that looks like
a CR (\r). Make that:
f = fopen (fname, "rb" );
Then (again unless this is a typo) you execute an fwrite() rather than an
fread() in read_board()! Make that:
fread(b->data, sizeof (my_struct), mapx * mapy, f);
All should then be fine.
Art S. Kagel, kagel AT bloomberg DOT com
- Raw text -