Chinaunix首页 | 论坛 | 博客
  • 博客访问: 350323
  • 博文数量: 89
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 951
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-23 11:51
个人简介

好懒,什么都没写

文章分类

全部博文(89)

文章存档

2012年(3)

2011年(17)

2007年(20)

2006年(49)

我的朋友

分类: C/C++

2011-06-19 14:30:09

数据定义
  1. class CJobInfo
  2. {
  3. public:
  4.     CJobInfo(void);
  5. public:
  6.     ~CJobInfo(void);

  7. public:
  8.     int storeToFile( FILE *pf);
  9.     int loadFromFile( FILE *pf);


  10. public:
  11.     std::string getName();
  12.     void setName(std::string name);

  13.     std::string getStatus();
  14.     void setStatus(std::string status);

  15.     std::string getProc();
  16.     void setProc(std::string proc);

  17.     std::string getPrt();
  18.     void setPrt(std::string prt);

  19. private:
  20.     std::string m_strName;
  21.     std::string m_strStatus;
  22.     std::string m_strProc;
  23.     std::string m_strPrt;

  24. };

  25. class CJobInfoStore
  26. {
  27. public:
  28.     CJobInfoStore(void);
  29. public:
  30.     ~CJobInfoStore(void);

  31. public:
  32.     int storeToFile( FILE *pf);
  33.     int loadFromFile( FILE *pf);

  34. public:
  35.     std::list<CJobInfo> getJobList();
  36.     std::string getCharcode();
  37.     void setCharcode(std::string charcode );
  38.     void addJob(CJobInfo jobInfo );

  39. private:
  40.     std::string m_strCharcode;
  41.     std::list<CJobInfo> m_jobList;
  42. };


以二进制形式输出数据到文件中
  1. int CJobInfo::loadFromFile(FILE *pf)
  2. {

  3.     int length = 0;
  4.     char buf[JOB_BUF_LEN];

  5.     memset(buf, 0, JOB_BUF_LEN );
  6.     fread( &length,sizeof(int),1, pf);
  7.     fread( buf, sizeof(char), length, pf );
  8.     m_strName = std::string( buf );

  9.     memset(buf, 0, JOB_BUF_LEN );
  10.     fread( &length,sizeof(int),1, pf);
  11.     fread( buf, sizeof(char), length, pf );
  12.     m_strStatus = std::string( buf );

  13.     memset(buf, 0, JOB_BUF_LEN );
  14.     fread( &length,sizeof(int),1, pf);
  15.     fread( buf, sizeof(char), length, pf );
  16.     m_strProc = std::string( buf );

  17.     memset(buf, 0, JOB_BUF_LEN );
  18.     fread( &length,sizeof(int),1, pf);
  19.     fread( buf, sizeof(char), length, pf );
  20.     m_strPrt = std::string( buf );

  21.     return 0;
  22. }

  23. int CJobInfoStore::storeToFile( FILE *pf)
  24. {
  25.     char buf[1024];
  26.     int listsize = m_jobList.size();
  27.     int length = 0;

  28.     fwrite( &listsize, sizeof(int), 1, pf );
  29.     std::cout << "list size = " << listsize << std::endl;

  30.     length = m_strCharcode.length();
  31.     fwrite( &length, sizeof(int), 1, pf );
  32.     fwrite( m_strCharcode.c_str(), sizeof(char), length, pf);
  33.     std::cout << "charcode = " << m_strCharcode << std::endl;



  34.     std::list<CJobInfo>::iterator it;

  35.     for ( it=m_jobList.begin(); it!=m_jobList.end(); it++ )
  36.     {
  37.         CJobInfo job;
  38.         job = *it;
  39.         job.storeToFile(pf);
  40.     }

  41.     return 0 ;
  42. }


以二进制形式从文件中读入数据
  1. int CJobInfo::storeToFile(FILE *pf)
  2. {

  3.     int length = 0;

  4.     length = m_strName.length();
  5.     fwrite( &length, sizeof(int), 1, pf );
  6.     fwrite( m_strName.c_str(), sizeof(char), length, pf);

  7.     length = m_strStatus.length();
  8.     fwrite( &length, sizeof(int), 1, pf );
  9.     fwrite( m_strStatus.c_str(), sizeof(char), length, pf);

  10.     length = m_strProc.length();
  11.     fwrite( &length, sizeof(int), 1, pf );
  12.     fwrite( m_strProc.c_str(), sizeof(char), length, pf);

  13.     length = m_strPrt.length();
  14.     fwrite( &length, sizeof(int), 1, pf );
  15.     fwrite( m_strPrt.c_str(), sizeof(char), length, pf);

  16.     return 0;

  17. }

  18. int CJobInfoStore::loadFromFile( FILE *pf)
  19. {
  20.     int length;
  21.     char buf[1024];

  22.     int listsize = 0;
  23.     fread( &listsize,sizeof(int),1, pf);
  24.     std::cout << "list size = " << listsize << std::endl;

  25.     memset( buf, 0, 1024);
  26.     fread( &length, sizeof(int), 1, pf );
  27.     fread( buf, sizeof(char), length, pf);
  28.     m_strCharcode = std::string(buf);
  29.     std::cout << "charcode = " << buf << std::endl;

  30.     for( int i=0; i<listsize; i++ )
  31.     {
  32.         CJobInfo job;
  33.         job.loadFromFile( pf );
  34.         
  35.         this->m_jobList.push_back(job);

  36.     }

  37.     return 0;
  38. }

阅读(1512) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~