fstream 将类写入文件

将类写入文件是比较通用的任务,是写文件索引的基础。进一步可以将b+ tree等东西写入文件
  1. /*
  2.  * =====================================================================================
  3.  *
  4.  *       Filename:  classtfile.cpp
  5.  *
  6.  *    Description:  将类写入文件,必要时候都回到内存中
  7.  *
  8.  *        Version:  1.0
  9.  *        Created:  2008年12月26日 10时34分50秒
  10.  *       Revision:  none
  11.  *       Compiler:  gcc
  12.  *
  13.  *         Author:  Li WeiJian (mn), lwj1396@163.com
  14.  *        Company:  hunan university
  15.  *
  16.  * =====================================================================================
  17.  */
  18. #include<iostream>
  19. #include<fstream>
  20. using namespace std;
  21. class person
  22. {
  23.     public:
  24.         enum {SIZE=50};
  25.         int id;
  26.         int age;
  27.         char name[SIZE];
  28.         char address[SIZE];
  29. };
  30. class Database
  31. {
  32.     private:
  33.         fstream fs;
  34.         string filename;
  35.         
  36.         void openf()
  37.         {
  38.             try
  39.             {
  40.                 fs.open(this->filename.c_str(),fstream::in|fstream::out|fstream::app);
  41.             }catch(...) 
  42.             {   }
  43.         }
  44.     public:
  45.         Database(string fn):filename(fn)
  46.         {
  47.         }
  48.         ~Database()
  49.         {
  50.             if(fs.is_open())
  51.                 fs.close();
  52.         }
  53.         //通过id查找位置
  54.         size_t query(size_t id)
  55.         {
  56.             size_t index=0; 
  57.             if(!fs.is_open())
  58.                 openf();
  59.             fs.seekg(0,ios_base::beg);
  60.             while(!fs.eof())
  61.             {
  62.                 fs.read(reinterpret_cast<char*>(&index),sizeof(int));
  63.                 if(index==id)
  64.                 {
  65.                     fs.seekg(-sizeof(int),ios_base::cur);
  66.                     return fs.tellg();
  67.                 }
  68.                 fs.seekg(sizeof(person)-sizeof(int),ios_base::cur);
  69.             }
  70.             fs.close();
  71.             
  72.             return -1;
  73.         }
  74.         //返回位置n的person记录
  75.         person retrieve(size_t n)
  76.         {
  77.             person p;
  78.             if(!fs.is_open())
  79.                 openf();
  80.             int resultindex=query(n);
  81.             if(resultindex!=-1)//找到了
  82.             {
  83.                 fs.seekg(resultindex);
  84.                 fs.read(reinterpret_cast<char*>(&p),sizeof(person));
  85.                 fs.close();
  86.                 return p;
  87.             }
  88.         
  89.             fs.close();
  90.             throw("no result");
  91.         }
  92.         
  93.         //修改位置n的person记录
  94.         int update(size_t n, person& p)
  95.         {
  96.             if(!fs.is_open())
  97.                 fs.open(filename.c_str(),fstream::in|fstream::out);
  98.             int resultindex=query(n);
  99.             if(resultindex!=-1)//找到了
  100.             {
  101.                 fs.seekp(resultindex);
  102.                 fs.write(reinterpret_cast<char*>(&p),sizeof(person));
  103.                 fs.close();
  104.                 return 0;
  105.             }
  106.             fs.close();
  107.             return 1;
  108.         }
  109.         //添加一个人的记录
  110.         void add(person& p)
  111.         {
  112.             if(!fs.is_open())
  113.                 openf();
  114.             fs.seekp(0,ios::end);
  115.             fs.write(reinterpret_cast<char*>(&p),sizeof(person));
  116.             fs.close();
  117.         }
  118. };
  119. int main()
  120. {
  121.     Database db("db");
  122.     person p;
  123.     p.id=1;
  124.     p.age=10;
  125.     strcpy(p.name,"person1");
  126.     strcpy(p.address,"hunan");
  127.     person p2;
  128.     p2.id=2;
  129.     p2.age=68;
  130.     strcpy(p2.name,"person2");
  131.     strcpy(p2.address,"guangxi");
  132.     person p3;
  133.     p3.id=3;
  134.     p3.age=333;
  135.     strcpy(p3.name,"person3");
  136.     strcpy(p3.address,"shenzhen");
  137.     db.add(p);
  138.     db.add(p2);
  139.     db.add(p3);
  140.     for(int i=0;i<3;i++)
  141.     {   
  142.         try
  143.         {
  144.             person p=db.retrieve(i+1);
  145.             cout<<"p.id="<<p.id<<endl;
  146.             cout<<"p.age="<<p.age<<endl;
  147.             cout<<"p.name="<<p.name<<endl;
  148.             cout<<"p.address="<<p.address<<endl;
  149.         }catch(...)
  150.         {
  151.             cout<<"can't not find the person "<<i+1<<endl;
  152.         }
  153.     }
  154.     cout<<"change 2 happend...."<<endl;
  155.     person ppp;
  156.     ppp.id=1;
  157.     ppp.age=222;
  158.     strcpy(ppp.name,"new person 1");
  159.     strcpy(ppp.address,"中华人们公共和国");
  160.     db.update(1,ppp);//修改
  161.     for(int i=0;i<3;i++)
  162.     {   
  163.         try
  164.         {
  165.             person p=db.retrieve(i+1);
  166.             cout<<"p.id="<<p.id<<endl;
  167.             cout<<"p.age="<<p.age<<endl;
  168.             cout<<"p.name="<<p.name<<endl;
  169.             cout<<"p.address="<<p.address<<endl;
  170.         }catch(...)
  171.         {
  172.             cout<<"can't not find the person "<<i+1<<endl;
  173.         }
  174.     }
  175. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值