Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using the ndbm library

Posted on 1997-05-19
7
Medium Priority
?
210 Views
Last Modified: 2013-12-26
Does anyone have any experience using the old ndbm library in UNIX?  I have a structure defined something like:
      char key[12];
      typedef struct {
            char name[50];
            char addr[50];
            char phone[12];
      } data;
How do I store and retrieve the data?  I've only used the library for singular strings.

Quick replies will be greatly appreciated.
0
Comment
Question by:walkerm
  • 4
  • 3
7 Comments
 
LVL 3

Accepted Solution

by:
dhm earned 200 total points
ID: 1292903
ndbm uses a structure called a datum to describe both keys and values.  The structure looks like this:

struct datum {
    char *dptr;
    int   dlen;
} ;

When you work with strings, you usually set dptr to point to the string, and dlen is strlen(dptr).  In this case, however, you just set dptr to point to your data structure, and dlen will be sizeof(data).  (Assuming the typedef you gave in the question.  The sizeof will probably be 112.)

So, here's some sample code:

DBM *database = dbm_open( /* filename, flags, etc */ );

char key[12];
data my_data;
datum K;
datum V;

memset( key, 0, sizeof(key) );
memset( &my_data, 0, sizeof(my_data) );

strcpy( key, "billgates" );
strcpy( my_data.name, "Bill Gates" );
strcpy( my_data.addr, "Redmond, WA" );
strcpy( my_data.phone, "800-WINDOZE" );

K.dptr = key;
K.dlen = strlen(key);
V.dptr = (char *)(&my_data);
V.dlen = sizeof(my_data);

db_store( database, K, V, DBM_REPLACE );


0
 

Author Comment

by:walkerm
ID: 1292904
Thanks. Now I need to use dbm_fetch to retrieve the data (assuming the same structure).

Thanks again
0
 

Author Comment

by:walkerm
ID: 1292905
Thanks. Now I need to use dbm_fetch to retrieve the data (assuming the same structure).  Please help

Thanks again
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:dhm
ID: 1292906
Continuing from the db_store() in my example:

V = db_fetch( database, K );

if (V.dptr == 0) { printf( "Not found!\n" ); exit( 0 ); }
if (V.dlen != sizeof(my_data)) { printf( "Wrong data size!\n"; }
memcpy( &my_data, V.dptr, sizeof(my_data) );

printf( "Phone number for %s is %s\n", my_data.name, my_data.phone );
0
 

Author Comment

by:walkerm
ID: 1292907
I'm getting a segmentation fault on the call to dbm_fetch. I have Digital UNIX V4.0B  (Rev. 564).

Any ideas?
0
 
LVL 3

Expert Comment

by:dhm
ID: 1292908
Here's a complete, working program that I compiled on Digital Unix 4.0, using both gcc-2.7.2 and the DEC C compiler.

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <ndbm.h>
#include <errno.h>
#include <fcntl.h>

typedef struct data {
  char       name[50];
  char       addr[50];
  char       phone[12];
} data;

int
main( int argc, char **argv )
{
  DBM      *database;
  datum       K;
  datum       V;
  char       key[12];
  data       my_data;
  int       result;

  database = dbm_open( "/tmp/test-db", O_RDWR | O_CREAT, 0666 );
  if (database == 0) {
    printf( "Can't open/create database: %d\n", errno );
    exit( errno );
  }

  strcpy( key, "billgates" );
  strcpy( my_data.name, "Bill Gates" );
  strcpy( my_data.addr, "Redmond, WA" );
  strcpy( my_data.phone, "800-WINDOZE" );

  K.dptr = key;
  K.dsize = strlen( key );
  V.dptr = &my_data;
  V.dsize = sizeof(my_data);

  result = dbm_store( database, K, V, DBM_REPLACE );
  printf( "dbm_store result = %d\n", result );

  V.dptr = 0;
  V.dsize = 0;
  V = dbm_fetch( database, K );

  if (V.dptr == 0) {
    printf( "I can't find the key I just inserted!\n" );
  } else {
    if (V.dsize != sizeof(my_data)) {
      printf( "Expected the value for this key to be %d bytes long, not %d\n",
            sizeof(my_data), V.dsize );
    } else {
      memcpy( &my_data, V.dptr, sizeof(my_data) );
      printf( "Name: %s Address %s Phone %s\n",
            my_data.name, my_data.addr, my_data.phone );
    }
  }

  printf( "Now, look for username \"marca\" (expect NOT FOUND)\n" );

  strcpy( key, "marca" );
  K.dptr = key;
  K.dsize = strlen( key );

  V.dptr = 0;
  V.dsize = 0;
  V = dbm_fetch( database, K );

  if (V.dptr == 0) {
    printf( "I can't find the key. (That's good!)\n" );
  } else {
    if (V.dsize != sizeof(my_data)) {
      printf( "Expected the value for this key to be %d bytes long, not %d\n",
            sizeof(my_data), V.dsize );
    } else {
      memcpy( &my_data, V.dptr, sizeof(my_data) );
      printf( "Name: %s Address %s Phone %s\n",
            my_data.name, my_data.addr, my_data.phone );
    }
  }

  dbm_close( database );

  exit( 0 );
}

0
 

Author Comment

by:walkerm
ID: 1292909
Thank you!!!  That really help me out.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question