六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 31|回复: 0

fstream 将类写入文件

[复制链接]

升级  72.35%

801

主题

801

主题

801

主题

探花

Rank: 6Rank: 6

积分
2447
 楼主| 发表于 2013-1-26 13:45:21 | 显示全部楼层 |阅读模式
将类写入文件是比较通用的任务,是写文件索引的基础。进一步可以将b+ tree等东西写入文件
<div class="highlighter">

  • /*
  • *=====================================================================================
  • *
  • *Filename:classtfile.cpp
  • *
  • *Description:将类写入文件,必要时候都回到内存中
  • *
  • *Version:1.0
  • *Created:2008年12月26日10时34分50秒
  • *Revision:none
  • *Compiler:gcc
  • *
  • *Author:LiWeiJian(mn),lwj1396@163.com
  • *Company:hunanuniversity
  • *
  • *=====================================================================================
  • */

  • #include<iostream>
  • #include<fstream>
  • usingnamespacestd;

  • classperson
  • {
  • public:
  • enum{SIZE=50};
  • intid;
  • intage;
  • charname[SIZE];
  • charaddress[SIZE];
  • };

  • classDatabase
  • {
  • private:
  • fstreamfs;
  • stringfilename;

  • voidopenf()
  • {
  • try
  • {
  • fs.open(this->filename.c_str(),fstream::in|fstream::out|fstream::app);
  • }catch(...)
  • {}
  • }
  • public:
  • Database(stringfn):filename(fn)
  • {
  • }

  • ~Database()
  • {
  • if(fs.is_open())
  • fs.close();
  • }


  • //通过id查找位置
  • size_tquery(size_tid)
  • {
  • size_tindex=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);
  • returnfs.tellg();
  • }
  • fs.seekg(sizeof(person)-sizeof(int),ios_base::cur);
  • }
  • fs.close();

  • return-1;
  • }


  • //返回位置n的person记录
  • personretrieve(size_tn)
  • {
  • personp;
  • if(!fs.is_open())
  • openf();
  • intresultindex=query(n);
  • if(resultindex!=-1)//找到了
  • {
  • fs.seekg(resultindex);
  • fs.read(reinterpret_cast<char*>(&p),sizeof(person));
  • fs.close();
  • returnp;
  • }

  • fs.close();
  • throw("noresult");
  • }


  • //修改位置n的person记录
  • intupdate(size_tn,person&p)
  • {
  • if(!fs.is_open())
  • fs.open(filename.c_str(),fstream::in|fstream::out);
  • intresultindex=query(n);
  • if(resultindex!=-1)//找到了
  • {
  • fs.seekp(resultindex);
  • fs.write(reinterpret_cast<char*>(&p),sizeof(person));
  • fs.close();
  • return0;
  • }
  • fs.close();
  • return1;
  • }


  • //添加一个人的记录
  • voidadd(person&p)
  • {
  • if(!fs.is_open())
  • openf();
  • fs.seekp(0,ios::end);
  • fs.write(reinterpret_cast<char*>(&p),sizeof(person));
  • fs.close();
  • }
  • };


  • intmain()
  • {
  • Databasedb("db");
  • personp;
  • p.id=1;
  • p.age=10;
  • strcpy(p.name,"person1");
  • strcpy(p.address,"hunan");
  • personp2;
  • p2.id=2;
  • p2.age=68;
  • strcpy(p2.name,"person2");
  • strcpy(p2.address,"guangxi");
  • personp3;
  • p3.id=3;
  • p3.age=333;
  • strcpy(p3.name,"person3");
  • strcpy(p3.address,"shenzhen");
  • db.add(p);
  • db.add(p2);
  • db.add(p3);
  • for(inti=0;i<3;i++)
  • {
  • try
  • {
  • personp=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'tnotfindtheperson"<<i+1<<endl;
  • }

  • }


  • cout<<"change2happend...."<<endl;
  • personppp;
  • ppp.id=1;
  • ppp.age=222;
  • strcpy(ppp.name,"newperson1");
  • strcpy(ppp.address,"中华人们公共和国");
  • db.update(1,ppp);//修改

  • for(inti=0;i<3;i++)
  • {
  • try
  • {
  • personp=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'tnotfindtheperson"<<i+1<<endl;
  • }

  • }
  • }

您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表