home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD1.iso / workshop / c / files / Roll.cpp next >
Encoding:
C/C++ Source or Header  |  2001-06-03  |  1.4 KB  |  91 lines

  1. #include <iostream>
  2. #include <fstream>
  3. #include "Roll.h"
  4.  
  5.  
  6. CRoll::CRoll(const std::string &fname)
  7. : m_fname(fname)
  8. , m_openmode(std::ios_base::binary)
  9. {
  10.    Read();
  11. }
  12.  
  13. CRoll::~CRoll()
  14. {
  15.    Write();
  16. }
  17.  
  18.  
  19. bool CRoll::Add(const CStudent &s)
  20. {
  21.    if (Find(s.StudentNo()))
  22.       return false;
  23.    m_data.push_back(s);
  24.    return true;
  25. }
  26.  
  27. CRoll::data_it CRoll::Get(unsigned studNo)
  28. {
  29.    data_it i;
  30.  
  31.    for (i=m_data.begin(); i!=m_data.end(); i++)
  32.       if (i->StudentNo()==studNo)
  33.          return i;
  34.    return m_data.end();
  35. }
  36.  
  37. bool CRoll::Find(unsigned studNo)
  38. {
  39.    return Get(studNo)!=m_data.end();
  40. }
  41.  
  42. bool CRoll::Find(unsigned studNo, CStudent &s)
  43. {
  44.    data_it i = Get(studNo);
  45.  
  46.    if (i==m_data.end())
  47.       return false;
  48.    s = *i;
  49.    return true;
  50. }
  51.    
  52. bool CRoll::Delete(unsigned studNo)
  53. {
  54.    data_it i = Get(studNo);
  55.  
  56.    if (i==m_data.end())
  57.       return false;
  58.    m_data.erase(i);
  59.    return true;
  60. }
  61.    
  62. bool CRoll::Read()
  63. {
  64.    std::ifstream fi(m_fname.c_str(), m_openmode);
  65.    if (!fi)
  66.       return false;
  67.    CStudent s;
  68.  
  69.    while (s.Read(fi))
  70.       Add(s);
  71.  
  72.    return m_data.size()>0;
  73. }
  74.  
  75. bool CRoll::Write()
  76. {
  77.    data_t::iterator i;
  78.    std::ofstream fo(m_fname.c_str(), m_openmode);
  79.    if (!fo)
  80.       return false;
  81.  
  82.    for (i=m_data.begin(); i!=m_data.end(); i++)
  83.       i->Write(fo);
  84.  
  85.    return fo.good();
  86. }
  87.  
  88.  
  89.  
  90.  
  91.