delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/19/10:41:55

Date: Tue, 19 Aug 1997 16:52:32 -0200
Message-Id: <199708191852.QAA005.81@ns1.bspu.ac.by>
To: <kah190 AT psu DOT edu>
From: Alexander Bokovoy <bokovoy AT bspu DOT ac DOT by>
Cc: DJGPP mailing list <djgpp AT delorie DOT com>
Subject: Re: Writing a struct to disk
MIME-Version: 1.0

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

You can use the technique which used in PASCAL:
TO WRITE:
 1. write [length] of string (include last zero character) as  int16 (in
many cases int16 enough)
 2. write string as a array of unsigned char
TO READ:
 1. read [length] of string as int16
 2. get [length] unsigned chars from heap 
 3. read into this memory block [length] chars from file.
-- 

Alexander Bokovoy, <bokovoy AT bspu DOT ac DOT by>
---== The Soft Age coming soon ==---

- Raw text -


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