将课程的源代码 使用C++写了一部分
LINUX WINDOW均可运行

#ifndef MYDB_H #define MYDB_H #include <iostream> #include <fstream> #include <string> #define NHASH_DEF 137 /* default hash table size */ #define FREE_LINK NHASH_DEF #define PTR_SZ 8 #define MAGIC_WORDS "MyDB_Start: " #define SEPARATOE "#" class MyDB{ public: MyDB(const char* pathName) : hashOffset(0), dataOffset(0),writeDataLength(0),idxOffset(0),writeIdxLength(0) { DBidxName = pathName; DBidxName += ".idx"; DBdataName = pathName; DBdataName += ".dat"; } void CloseDB(){ idxFile.close(); datFile.close(); } ~MyDB(){ CloseDB();} bool Init(); unsigned long HashFunc(const char *key); bool WriteData(const char* data); bool WriteIndex(const char* key); size_t GetIndexOffsetBykey(const char* key); long ReadPtr(std::fstream& fs, size_t offset); bool Writeptr(std::fstream& fs, size_t offset, const std::string& writeString); void TestFunc(const char* searchKey); size_t SearchLinkForKey(size_t offset, const char* key); std::string Query(const char* key); private: bool DoWriteIndex(const char* key, const size_t nextOffset); bool OpenFileOrExit(std::fstream& fs, const std::string& fileName); size_t hashOffset; size_t dataOffset; size_t writeDataLength; unsigned long idxOffset; size_t writeIdxLength; MyDB& operator=(const MyDB&); MyDB(const MyDB&); std::string DBidxName; std::string DBdataName; std::fstream idxFile; std::fstream datFile; }; #endif // MYDB_H

#include <string> #include "mydb.h" void MyDB::TestFunc(const char* key) { } std::string MyDB::Query(const char* key) { size_t offset = GetIndexOffsetBykey(key); if (0 == offset) return ""; size_t nextPtr = ReadPtr(idxFile, offset); size_t indexLength = ReadPtr(idxFile, offset + PTR_SZ); char* indexstr = new char[indexLength + 1]; idxFile.read(indexstr, indexLength); indexstr[indexLength] = '