TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Geoff Wilson
from: David Nugent
date: 1996-01-05 02:08:16
subject: Linked Lists

GW>         The problem I have is this, what is the best
 GW> method of saving the data to disk and/or reading it.
 GW> I'm pretty sure that it isn't a good idea to save the
 GW> pointer info on the disk and then reload it because DOS
 GW> mightn't allocate it the same space. Should I save each
 GW> item of the structure individually or should I create a
 GW> new structure just for storing the info on the disk or
 GW> what?
 GW>         In case it helps, this is the structure I am using:

 GW> struct book{
 GW>         char title[40];
 GW>         char author[25];
 GW>         int year;
 GW>         char type_1;
 GW>         char type_2;
 GW>         struct book *next;
 GW> }

Make this:

struct book {
    char title[40];
    char author[25];
    int year;
    char type_1, type_2;
};

Now, when you manipulate these in memory:

struct s_book {
    struct s_book * next;
    struct book thisbook;
};

In other words, you use a nested structure.

When you manipulate these on disk - assuming you want a flat fixed-record
length file with nothing clever in it like freelists and so forth - then
just read them as "struct book", allocating struct s_book's on
the fly using malloc() or friends.

You can fairly easily make the `container' (here, represented by a linked
list) almost entirely a generic entity for manipulating any sort of linked
list. In some situations - namely, when reporting and so forth - it might
be more convenient to deal with this as an array (or, if you get fancy, a
virtual array where you page the data from disk instead of loading it all
into memory), and the method I suggest would allow you to handle the
database in that manner as well.

--- MaltEd/2 1.0.b6
* Origin: Decadence BBS & Unique Computing - What a team! (3:632/348)
SEEN-BY: 50/99 620/243 623/630 632/103 348 360 998 633/371 634/384 388
SEEN-BY: 635/301 502 503 544 727 639/252 711/401 409 410 413 430 510 808 809
SEEN-BY: 711/932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 632/348 635/503 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.