将类写入文件是比较通用的任务,是写文件索引的基础。进一步可以将b+ tree等东西写入文件
- /*
- * =====================================================================================
- *
- * Filename: classtfile.cpp
- *
- * Description: 将类写入文件,必要时候都回到内存中
- *
- * Version: 1.0
- * Created: 2008年12月26日 10时34分50秒
- * Revision: none
- * Compiler: gcc
- *
- * Author: Li WeiJian (mn), lwj1396@163.com
- * Company: hunan university
- *
- * =====================================================================================
- */
- #include<iostream>
- #include<fstream>
- using namespace std;
- class person
- {
- public:
- enum {SIZE=50};
- int id;
- int age;
- char name[SIZE];
- char address[SIZE];
- };
- class Database
- {
- private:
- fstream fs;
- string filename;
- void openf()
- {
- try
- {
- fs.open(this->filename.c_str(),fstream::in|fstream::out|fstream::app);
- }catch(...)
- { }
- }
- public:
- Database(string fn):filename(fn)
- {
- }
- ~Database()
- {
- if(fs.is_open())
- fs.close();
- }
- //通过id查找位置
- size_t query(size_t id)
- {
- size_t index=0;
- if(!fs.is_open())
- openf();
- fs.seekg(0,ios_base::beg);
- while(!fs.eof())
- {
- fs.read(reinterpret_cast<char*>(&index),sizeof(int));
- if(index==id)
- {
- fs.seekg(-sizeof(int),ios_base::cur);
- return fs.tellg();
- }
- fs.seekg(sizeof(person)-sizeof(int),ios_base::cur);
- }
- fs.close();
- return -1;
- }
- //返回位置n的person记录
- person retrieve(size_t n)
- {
- person p;
- if(!fs.is_open())
- openf();
- int resultindex=query(n);
- if(resultindex!=-1)//找到了
- {
- fs.seekg(resultindex);
- fs.read(reinterpret_cast<char*>(&p),sizeof(person));
- fs.close();
- return p;
- }
- fs.close();
- throw("no result");
- }
- //修改位置n的person记录
- int update(size_t n, person& p)
- {
- if(!fs.is_open())
- fs.open(filename.c_str(),fstream::in|fstream::out);
- int resultindex=query(n);
- if(resultindex!=-1)//找到了
- {
- fs.seekp(resultindex);
- fs.write(reinterpret_cast<char*>(&p),sizeof(person));
- fs.close();
- return 0;
- }
- fs.close();
- return 1;
- }
- //添加一个人的记录
- void add(person& p)
- {
- if(!fs.is_open())
- openf();
- fs.seekp(0,ios::end);
- fs.write(reinterpret_cast<char*>(&p),sizeof(person));
- fs.close();
- }
- };
- int main()
- {
- Database db("db");
- person p;
- p.id=1;
- p.age=10;
- strcpy(p.name,"person1");
- strcpy(p.address,"hunan");
- person p2;
- p2.id=2;
- p2.age=68;
- strcpy(p2.name,"person2");
- strcpy(p2.address,"guangxi");
- person p3;
- p3.id=3;
- p3.age=333;
- strcpy(p3.name,"person3");
- strcpy(p3.address,"shenzhen");
- db.add(p);
- db.add(p2);
- db.add(p3);
- for(int i=0;i<3;i++)
- {
- try
- {
- person p=db.retrieve(i+1);
- cout<<"p.id="<<p.id<<endl;
- cout<<"p.age="<<p.age<<endl;
- cout<<"p.name="<<p.name<<endl;
- cout<<"p.address="<<p.address<<endl;
- }catch(...)
- {
- cout<<"can't not find the person "<<i+1<<endl;
- }
- }
- cout<<"change 2 happend...."<<endl;
- person ppp;
- ppp.id=1;
- ppp.age=222;
- strcpy(ppp.name,"new person 1");
- strcpy(ppp.address,"中华人们公共和国");
- db.update(1,ppp);//修改
- for(int i=0;i<3;i++)
- {
- try
- {
- person p=db.retrieve(i+1);
- cout<<"p.id="<<p.id<<endl;
- cout<<"p.age="<<p.age<<endl;
- cout<<"p.name="<<p.name<<endl;
- cout<<"p.address="<<p.address<<endl;
- }catch(...)
- {
- cout<<"can't not find the person "<<i+1<<endl;
- }
- }
- }