Date: Wed, 27 Aug 1997 18:02:55 -0500 (CDT) From: Andrew Deren To: Paul Derbyshire cc: djgpp AT delorie DOT com Subject: Re: Boy do I feel STUPID!! ....Files question... In-Reply-To: <5tos0p$pdf@freenet-news.carleton.ca> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On 24 Aug 1997, Paul Derbyshire wrote: > > "Locke" (lockeg AT usa DOT net) writes: > > Hey everyone, > > > > i am feeling really stupid, but i need help. I have been working on this > > game and it's going verrry well, but i've hit a wall. I can't believe > > this, but i can't remember how to write an array of structures to a file! > > > > for instance... say i have a structure called S_MAP it goes as follows: > > > > typedef struct > > { > > char *name; > > int tiles[MAX_Y][MAX_X]; > > } S_MAP; > > > > S_MAP Maps[MAX_MAPS]; > > > > > > That's a very watered down version of the structure, but it gets the idea > > across... > > Anyway, i want to write the variable Maps to a file and then later read it > > back in. I can't believe that I can write low level graphics routines, a > > keyboard handler and all this really complicated stuff, but can't remember > > how to write to a file. Well, any help would be greatly appreceated... > > fwrite(Maps,sizeof(S_MAP),MAX_MAPS,my_file_pointer_returned_by_fopen); But you won't save char *name part of the structure. You only will save a pointer to that name. I would consider changing declarations in S_MAP to char name[15]; or whatever is the maximum number of chars in name. You have to be careful about saving structs to disk. You have to watch out for pointers inside the struct. > > Two remarks: one: this is documented in the info files, info libc a > fwrite. Two, this is a general C question, and might be better placed in > comp.lang.c. > -- > .*. Where feelings are concerned, answers are rarely simple [GeneDeWeese] > -() < When I go to the theater, I always go straight to the "bag and mix" > `*' bulk candy section...because variety is the spice of life... [me] > Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh > ,,, (0 0) +-------------oOO----(_)-------------------+ | Andrew Deren | | aderen AT eecs DOT uic DOT edu | | www.eecs.uic.edu/~aderen/ader/main.html | +-------------------------oOO--------------+ || || ooO Ooo