www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/24/12:50:20

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Writing a struct to disk
Date: Sun, 24 Aug 1997 04:13:40 -0700
Organization: Alcyone Systems
Lines: 33
Message-ID: <34001764.62941D25@alcyone.com>
References: <33F93687 DOT 14CE173 AT psu DOT edu>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Kertis A. Henderson wrote:

> 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.

Neither could the char * for the name, hence your question.

There are a number of ways you could do it.  For the string, you could
write the string followed by a NUL character, or (more often useful)
write the number of characters followed by them.  This is more
convenient, especially in your case, because then you know beforehand
the number of bytes to allocate when reading it back in.

As for the node pointers, likely you would just want to write out these
nodes individually in the order in which they are linked, with either a
token to indicate when there aren't anymore (should that be convenient),
or just start the file with a count of the number of nodes.

-- 
          Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
                        Alcyone Systems / http://www.alcyone.com/max/
   San Jose, California, United States / icbm://37.20.07n/121.53.38w
                                      \
                  "War is like love; / it always finds a way."
                                    / Bertolt Brecht

- Raw text -


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