NDBM and ME

Adrian J Ho adrianho at barkley.berkeley.edu
Sat May 11 11:31:37 AEST 1991


In article <GUEST.91May10124944 at geech.ai.mit.edu> guest at geech.ai.mit.edu (Guest Account) writes:
>I read the manual on ndbm but I still have a question.  I want to
>store structs and the structs have pointers to variable length
>strings.  If I store the struct using the data.dptr how will the
>pointers remain valid after closing the database file?

They don't -- you're screwed once your program terminates.

>							I don't really
>care how I do it, I just want to be able to use some kind of database
>management package on a struct like
>	   struct system_entry {
>		   char *system_name;
>		   char *telephone_number;
>		   char *login_name;
>		   char *password;
>	   }

>I won't know how long the strings are at compile time.

You don't have to -- just malloc() a string that's the total size of
all 4 fields (plus 4 for their associated nulls), then use strcpy()
to concatenate all 4 fields into the new string (be careful of the
NULLs -- that's why I didn't say strcat()).

You now have a single string with all 4 fields that can be used in an
NDBM record.  I'll leave the reverse process for you to figure out.

Oh, and if you _must_ use a guest account for some reason, please
leave a non-guest email address in your message, so that others can
reply to you directly.

Good luck!



More information about the Comp.unix.programmer mailing list