delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/07/12/07:33:19

Message-ID: <33C76A7A.15E4@swipnet.se>
Date: Sat, 12 Jul 1997 13:28:58 +0200
From: Deltaman <deltaman AT swipnet DOT se>
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: My structures get broken when I fwrite

Hi!

I have a program, sort of an editor. Its data structures is like this:

#define mapx            20
#define mapy            15

typedef struct {
	int	type;
	int	back;
	int	attr;
} my_struct;

typedef struct {
	my_struct	 data [mapx][mapy];
} map;


When I fill the structures with data it works OK, but when I save a map
and reload it it get broken. About half of the data gets lost/corrupted.
These are the functions I use to read/write data:

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);
	}

I know the data is correct before saving it, so I guess something in
these functions must be wrong. Can any kind soul please help me. TIA!
-- 
Deltaman (deltaman AT swipnet DOT se)
http://www.geocities.com/TimesSquare/8240

- Raw text -


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