Q:why didn't the following code creat the "mima.db" -
04-16-2006
, 12:49 AM
note:there's no compile,build,run error!and these codes are build in VC++ 6.0 in windows
here's the code:
//mima.h
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//only this head should include for use bdb.
#include "db_cxx.h"
#include "db.h"
class Mima
{
private:
Db mimadb;
//u_int32_t oFlags;
//Mima() : mimadb(NULL, 0) {}
void close();
char *mima;
Dbt pkey;
Dbt pdata;
char *yonghuming;
public:
Mima();
// Our destructor just calls our private close method.
~Mima() { close(); }
//写入密码
// put();//不需要写入操作
//读出密码
get();
//修改密码//修改的时候先删除密码,再写入操作
//xiugai(str::xinmima);
};
//mima.cpp
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//only this head should include for use bdb.
#include "mima.h"
//implementation for the constructor
Mima :: Mima()
: mimadb(NULL, 0), // Instantiate Db object
//oFlags(DB_CREATE),
pkey(&mima, sizeof(char)),
pdata(yonghuming, strlen(yonghuming) + 1)
// mima(123456)
{
try
{
mimadb.open(NULL, // Transaction pointer
"mima.db", // Database file name
NULL, // Optional logical database name
DB_BTREE, // Database access method
DB_CREATE, // Open flags
0); // File mode (using defaults)
}
catch(DbException &e)
{
// Error handling code goes here
std::cerr << e.what() << std::endl;
}
catch(std::exception &e)
{
// Error handling code goes here
std::cerr << e.what() << std::endl;
}
char *yonghuming = "lwu";
char *mima="123456";
mimadb.put(NULL, &pkey, &pdata, DB_NOOVERWRITE);//初始化密码"123456"
printf("db: %s: data stored.\n", pdata.get_data());
}
void Mima::close()
{
// Close the mimadb
try
{
mimadb.close(0);
// DbException is not subclassed from std::exception, so
// need to catch both of these.
}
catch(DbException &e)
{
std::cerr << e.what() << std::endl;
}
catch(std::exception &e)
{
std::cerr << e.what() << std::endl;
}
}
//Mima:: put()
//{
// char *yonghuming = "lwu";
// mima="123456";
// Dbt pkey(&mima, sizeof(char));
// Dbt pdata(yonghuming, strlen(yonghuming) + 1);
// mimadb.put(NULL, &pkey, &pdata, DB_NOOVERWRITE);
//}
Mima::get()
{
mimadb.get(NULL, &pkey, &pdata, 0);
}
void main()
{
Mima a();
}
//codes end
thanks great master. |