Advertisement

05.04.2004 at 07:27PM PDT, ID: 20978186
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Storing C/C++ structures/objects

Zone: Berkeley DB
Tags: structure, db, berkeley
Can someone provide a complete example on storing C structures to Berkeley DB?  I having problems with the examples from the SleepyCat website.


int add(void)
{
      size_t len;
      u_int8_t *p, data_buffer[1024];

      DBT data, key;
      DB *dbp;
      int ret;
      long SerialNumber = 1989;

      info.data1 = "Hello World";
      info.data2 = 500;

      memset(&key, 0, sizeof(DBT));
      memset(&data, 0, sizeof(DBT));

      key.data = &SerialNumber;
      key.size = sizeof(SerialNumber);


      p = &data_buffer[0];
      len = strlen(info.data1);
      memcpy(p, &len, sizeof(len));
      p += sizeof(len);
      memcpy(p, info.data1, len);
      p += len;
      memcpy(p, &info.data2, sizeof(info.data2));
      p += sizeof(info.data2);


      data.data = &p;
      data.size = sizeof(p);

      if ((ret = db_create(&dbp, NULL, 0)) != 0)
      {
            fprintf(stderr, "db_create: %s\n", db_strerror(ret));
            return(ret);
      }
      if ((ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
      {
            dbp->err(dbp, ret, "%s", DATABASE);
            return(ret);;
      }

      if ((ret = dbp->put(dbp, NULL, &key, &data, 0)) == 0)
      {
            printf("db: %lu: key stored.\n", *(unsigned long *)key.data);
      }
      else
      {
            dbp->err(dbp, ret, "DB->put");
            return(ret);
      }

      ret = dbp->close(dbp, 0);



      return(ret);


}


int get(void)
{
      size_t len;
      u_int8_t *p, data_buffer[1024];

      DBT data, key;
      DB *dbp;
      int ret;

      long SerialNumber = 1989;



      if ((ret = db_create(&dbp, NULL, 0)) != 0)
      {
            fprintf(stderr, "db_create: %s\n", db_strerror(ret));
            return(ret);
      }
      if ((ret = dbp->open(dbp, NULL, DATABASE, NULL, DB_BTREE, DB_CREATE, 0664)) != 0)
      {
            dbp->err(dbp, ret, "%s", DATABASE);
            return(ret);;
      }


      memset(&key, 0, sizeof(DBT));
      memset(&data, 0, sizeof(DBT));


      key.data = &SerialNumber;
      key.size = sizeof(SerialNumber);




      if ((ret = dbp->get(dbp, NULL, &key, &data, 0)) == 0)
      {
            printf("db: %lu: key retrieved: data was %s.\n",
                *(unsigned long *)key.data, (char *)data.data);
      }
      else
      {
            //dbp->err(dbp, ret, "DB->get");
            return(ret);

      }

      strcpy(data_buffer,data.data);
      p = &data_buffer[0];
      memcpy(&len, p, sizeof(len));
      p += sizeof(len);
      info.data1 = malloc(len);
      memcpy(info.data1, p, len);
      p += len;
      memcpy(&info.data2, p, sizeof(info.data2));
      p += sizeof(info.data2);


      printf("%s\n",info.data1);

      return(0);

}


The examples do not really show how to store and receive the data structure so I have no way to determine if I am doing it correct.

Start your free trial to view this solution
Question Stats
Zone: Database
Question Asked By: marcus_carey
Solution Provided By: fridom
Participating Experts: 1
Solution Grade: A
Views: 11
Translate:
Loading Advertisement...
05.07.2004 at 12:42AM PDT, ID: 11012702

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 05:08AM PDT, ID: 11013862

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 07:00AM PDT, ID: 11014820

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 07:04AM PDT, ID: 11014869

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 07:19AM PDT, ID: 11014995

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 08:27AM PDT, ID: 11015762

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 08:33AM PDT, ID: 11015814

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 09:13AM PDT, ID: 11016195

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 09:39AM PDT, ID: 11016389

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
05.07.2004 at 10:41AM PDT, ID: 11016853

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.07.2004 at 09:29PM PST, ID: 12770798

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.07.2004 at 11:52PM PST, ID: 12771248

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.08.2004 at 06:34AM PST, ID: 12773552

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
12.08.2004 at 09:09AM PST, ID: 12775302

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29