delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/08/23/00:50:25

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Writing a struct to disk
Date: Tue, 19 Aug 1997 21:32:08 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 71
Message-ID: <33FA10D8.7994@cs.com>
References: <33F93687 DOT 14CE173 AT psu DOT edu>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp207.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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.

The standard C library supports many means of writing data to a disk
file.  You must first decide what format you want the data in:

Binary format has the advantage of size and speed.  It's faster to read
and write data in binary format, and it is compact.  The disadvantage is
that it is seldom portable from compiler to compiler, much less machine
to machine, because different compilers may use different data sizes or
struct member alignments.  Binary data is also secure from casual
tampering.

Text format has the advantage of being highly portable.  You can pass a
text formatted data file from system to system with the assurance that
it will be read in the same way by your programs.  It's slower than
binary format though, less secure, and requires more work to maintain
its integrity.

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

Assuming that you choose binary format, writing any kind of variable
length data is simple:  just write the size of the data first, followed
by the data itself.  When loading the data, read the size, followed by
that many bytes of data.  Sample code (assuming an open file pointer and
a variable 'data' of type 'struct node *'):

    int len;

    fwrite( &data->number, sizeof(data->number), 1, fp );
    fwrite( &data->age, sizeof(data->age), 1, fp );
    len = strlen( data->name ) + 1;  /* allow for null char */
    fwrite( &len, sizeof(len), 1, fp );
    fwrite( data->name, sizeof(char), len, fp );


To read it back:

    int len;

    /*...*/
    fread( &len, sizeof(len), 1, fp );
    data->name = malloc( len * sizeof(char) );
    fread( data->name, sizeof(char), len, fp );

Please note that this is just the basic idea; you may need to develop a
more specific solution for your specific needs.

hth!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "A generation which ignores history  |
|       aka Fighteer I       | has no past--and no future."         |
|   mailto:fighteer AT cs DOT com   |                                      |
| http://www.cs.com/fighteer |                - Lazarus Long        |
---------------------------------------------------------------------

- Raw text -


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