Date: Mon, 14 Jul 1997 11:46:39 -0400 (EDT) From: "Art S. Kagel" To: Deltaman Cc: djgpp AT delorie DOT com Subject: Re: My structures get broken when I fwrite In-Reply-To: <33C76A7A.15E4@swipnet.se> Message-Id: Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk 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