From: Thomas Demmer Newsgroups: comp.os.msdos.djgpp Subject: Re: Writing a struct to disk Date: Tue, 19 Aug 1997 14:32:20 +0200 Organization: Lehrstuhl fuer Stroemungsmechanik Lines: 54 Message-ID: <33F99254.6BED9CF1@LSTM.Ruhr-UNI-Bochum.De> References: <33F93687 DOT 14CE173 AT psu DOT edu> NNTP-Posting-Host: c64.lstm.ruhr-uni-bochum.de Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit CC: kah190 AT psu DOT edu To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Precedence: bulk Kertis A. Henderson wrote: > > Under djgpp, what is the best way to write a bunch of structs to a > file? For instance, I'd like to write a linked list to a file node by > node, then be able to read the nodes back and reconstruct the list. A > node could be something like the following: > > struct node { > int number; > unsigned char age; > char *name; > node *next; > } > > The ints and chars would be relatively easy, but I don't see how a > variable length string could be written. Of course, the node pointer > wouldn't be written. > > If anybody could help me out (or possibly point me in the right > direction), I'd appreciate it. Thanks for any input. What about this: int sl; ... sl= strlen(nd->name); fwrite(&sl,1,sizeof(sl),f); fwrite(nd->name,sl,sizeof(char),f); ... Read: int sl; ... fread(&sl,1,sizeof(sl),f); nd->name = malloc(sl); fread(nd->name,sl,sizeof(char),f); Check the order of parameters of fwrite() and and fread(), because I usually mix them up... -- Ciao Tom ************************************************************* * Thomas Demmer * * Lehrstuhl fuer Stroemungsmechanik * * Ruhr-Uni-Bochum * * Universitaetsstr. 150 * * D-44780 Bochum * * Tel: +49 234 700 6434 * * Fax: +49 234 709 4162 * * http://www.lstm.ruhr-uni-bochum.de/~demmer * *************************************************************