delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/14/11:48:23

Date: Mon, 14 Jul 1997 11:46:39 -0400 (EDT)
From: "Art S. Kagel" <kagel AT ns1 DOT bloomberg DOT com>
To: Deltaman <deltaman AT swipnet DOT se>
Cc: djgpp AT delorie DOT com
Subject: Re: My structures get broken when I fwrite
In-Reply-To: <33C76A7A.15E4@swipnet.se>
Message-Id: <Pine.D-G.3.91.970714114213.10079D-100000@dg1>
Mime-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019