>If any of you have used the ndbm/dbm database library
>that comes with most versions of UNIX, please anwer these
>questions:
> 1. How do you create keys.
> 2. What about the flags and modes for the functions
dbm is a key-valu-pair database. What I mean by that is that you
store things by a key, and later, given the key, you can get them
back again. A more common example of a key-value-pair database is a
filesystem: you store data in a file, and the key is the filename.
Later, you can type in the filename and get the data back.
So.
Here is some untested code:
#include <stdio.h>
#include <ndbm.h>
typedef struct {
char *dptr;
int dsize;
Quote:} datum;
void
main()
{
datum dbmKey, dbmContent;
DBM *dbm;
dbm = dbm_open("boys", O_RDWR|O_CREAT, 0664);
if (!dbm) {
perror("couldn't open dbm file");
exit(1);
}
dbmKey.dptr = "Simon";
dbmKey.dsize = strlen("Simon") + 1; /* i.e. sizeof("Simon"), incl. NUL */
dbmContent.dptr = "A good and close friend of mine.";
dbmContent.dsize = strln(dbmContent.dptr) + 1; /* include the NUL */
(void) dbm_store(dbm, key, content, DBM_REPLACE);
/* fetch it back again */
dbmContent = dbm_fetch(dbm, dbmKey);
printf("%*.*s: %*.*s\n",
dbmKey.dsize, dbmKey.dsize, dbmKey.dptr,
dbmContent.dsize, dbmContent.dsize, dbmContent.dptr
);
dbm_close(dbm);
return 0;
Quote:}
You can use anything you like for the key.
Any good book on Unix programming (Richard Stevens?) should discuss ndbm,
although I don't know which ones actually do.
Lee
--
Liam Quin, SoftQuad Inc | lq-text freely available Unix text retrieval
SGML: http://www.sq.com/ |`Consider yourself... one of the family...
The barefoot programmer | consider yourself... At Home!' [the Artful Dodger]