[Nemiver-list] Variables mixed up



Hello,

I have a problem with debugging a small program that I built. The program variables seem to be mixed up. I have reviewed my code and I don't think it's my program's bug, however you may review the code if you wish.

Running Ubuntu 7.04.
Compiled with "gcc -ldb-4.5 -g -o database database.c".
Requires libdb-4.5.

The value exportkey.data should be the value of exportdata.data.
The program, when compiled, does what it's meant to.

Thankyou,
David S
#include "db.h"
#include "string.h"

int main()
	{
	DB *dbp;           /* DB structure handle */
	u_int32_t flags;   /* database open flags */
	int ret;           /* function return value */
	DBT exportkey, exportdata, newdata;
	int key;
	char data[21] = {"This is text! (wow!)"};

	/* Initialize the structure. This
	 * database is not opened in an environment, 
	 * so the environment pointer is NULL. */
	ret = db_create(&dbp, NULL, 0);
	if (ret != 0)
		{
		puts("cannot create DB\n");
		return 0;
		}
	
	/* Database open flags */
	flags = DB_CREATE;    /* If the database does not exist, 
	                       * create it.*/

	/* open the database */
	ret = dbp->open(dbp,        /* DB structure pointer */
	                NULL,       /* Transaction pointer */
	                "testing.db", /* On-disk file that holds the database. */
	                NULL,       /* Optional logical database name */
	                DB_BTREE,   /* Database access method */
	                flags,      /* Open flags */
	                0);         /* File mode (using defaults) */
	if (ret != 0)
		{
		puts(db_strerror(ret));
		return 0;
		}
	key = 1;
	memset(&exportkey, 0, sizeof(DBT));
	memset(&newdata, 0, sizeof(DBT));
	memset(&exportdata, 0, sizeof(DBT));
	exportkey.data = &key;
	exportkey.size = sizeof(key);
	exportdata.data = &data;
	exportdata.size = 21;
	
	ret = dbp->put(dbp,NULL,&exportkey,&exportdata,0);
	if (ret != 0)
		{
		puts(db_strerror(ret));
		return 0;
		}
	
	ret = dbp->get(dbp,NULL,&exportkey,&newdata,0);
	if(ret == 0)
		{
		puts(newdata.data);
		}
	else
		{
		puts(db_strerror(ret));
		return 0;
		}
		
	/* When we're done with the database, close it. */
	if (dbp != NULL)
		{
		ret = dbp->close(dbp, 0); 
		if (ret != 0)
			{
			puts(db_strerror(ret));
			return 0;
			}
		}
	return 1;
	}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]