zoukankan      html  css  js  c++  java
  • MFC框架程序实现九

    CWinApp theApp;

    using namespace std;
    //文件操作
    void CFileExample()
    {
       CFile file;
       //打开或者新建文件
       BOOL bRet=file.Open("c:\\mfcfile.txt",
        CFile::modeCreate|CFile::modeReadWrite);
       if (!bRet)return;
       //文件的读写操作
       try
       {
           file.Write("Hello CFile",11);
        //移动文件指针到文件的开始位置
        file.SeekToBegin();
        char szText[100]={0};
        file.Read(szText,100);
        printf("%s\n",szText);


       }
       catch (CFileException* e)
       {
           //具体的异常处理
       }
       //文件关闭
       file.Close();
      

    }
    //修改文件状态信息
    void FileStatusExample()
    {
      CFileStatus status;
      CFile::GetStatus("c:\\mfcfile.txt",status);
      CTimeSpan span(366,0,0,0);
      status.m_ctime-=span;
      CFile::SetStatus("c:\\mfcfile.txt",status);


    }
    //文件查找
    void FileFindExample(CString strPath)
    {
       CFileFind find;
       //开始查找
       BOOL bRet=find.FindFile(strPath+"*.*");
       while(bRet)
       {
         //查找下一个文件
         bRet=find.FindNextFile();
      //获取文件信息
      CString strName=find.GetFileName();
      //获取文件路径
         strPath=find.GetFilePath();
      if (find.IsDirectory()&&!find.IsDots())
      {
       printf("Directory:%s\n",strName);
             FileFindExample(strPath+"\\*.*");
      }
      else
      {
       printf("File:%s\n",strName);

      }
       }
       //结束查找
       find.Close();
    }
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {

        //CFileExample();
     //FileStatusExample();
     FileFindExample("C:\\");
     return 0;
    }


    CWinApp theApp;

    using namespace std;
    //定义支持序列化的类
    class CStudent:public CObject
    {
    public:
     CStudent();
     CStudent(CString name,UINT age,CString hobby);
     void Show();
        virtual void Serialize( CArchive& ar );
     //DECLARE_SERIAL(CStudent)
     //动态创建宏
     _DECLARE_DYNCREATE(CStudent)
        //友元函数
     AFX_API friend CArchive& AFXAPI
     operator>>(CArchive& ar, CStudent* &pOb);
     

    public:
     CString m_strName;
     UINT m_nAge;
     CString m_strHobby;
    };
    //IMPLEMENT_SERIAL(CStudent,CObject,1)
    CObject* PASCAL CStudent::CreateObject()
    {
     return new CStudent;
    }
    _IMPLEMENT_RUNTIMECLASS(CStudent, CObject, 1,
          CStudent::CreateObject)

    AFX_CLASSINIT _init_CStudent(RUNTIME_CLASS(CStudent));

    CArchive& AFXAPI operator>>(CArchive& ar, CStudent* &pOb)
    {
     pOb = (CStudent*) ar.ReadObject(RUNTIME_CLASS(CStudent));
     return ar;
    }

    void CStudent::Serialize( CArchive& ar )
    {
     if (ar.IsStoring())
     {
      ar<<m_strName<<m_nAge<<m_strHobby;
     }
     else
     {
      ar>>m_strName>>m_nAge>>m_strHobby;
     }
    }
    CStudent::CStudent(){};
    CStudent::CStudent(CString name,UINT age,CString hobby)
    {
     m_strName=name;
     m_nAge=age;
     m_strHobby=hobby;
    }
    void CStudent::Show()
    {
     printf("姓名:%s\n",m_strName);
     printf("年龄:%d\n",m_nAge);
     printf("爱好:%s\n",m_strHobby);
    }
    ////////////////////////////////
    void ObjStore(CStudent& stu)
    {
      CFile file;
      file.Open("c:\\stu.dat",
       CFile::modeCreate|CFile::modeWrite);
      CArchive ar(&file,CArchive::store);
      ar<<&stu;
      ar.Close();
      file.Close();
    }
    void ObjLoad()
    {
      CFile file;
      file.Open("c:\\stu.dat",CFile::modeRead);
      CArchive ar(&file,CArchive::load);
      CStudent *pStu=NULL;
      ar>>pStu;
      ar.Close();
      file.Close();
      if (pStu)
      {
       pStu->Show();
      }

    }
    //////////////////////////////////////////////////
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {
        CStudent stu("张三",18,"足球");
        ObjStore(stu);
     ObjLoad();
     return 0;
    }

    CWinApp theApp;

    using namespace std;
    //数据存储
    void Store()
    {
       CFile file;
       BOOL bRet=file.Open("c:\\serial.dat",
        CFile::modeCreate|CFile::modeWrite);
       if (!bRet)
       {
        return;
       }
       CArchive ar(&file,CArchive::store);
       ar<<100;
       float fValue=12.25;
       ar<<fValue;
       CString strValue="Hello CArchive";
       ar<<strValue;
       ar.Close();
       file.Close();

    }
    void Load()
    {
     CFile file;
     file.Open("c:\\serial.dat",CFile::modeRead);
     CArchive ar(&file,CArchive::load);
        int nNum=0;
     ar>>nNum;
     float fNum=0.0;
     ar>>fNum;
     CString strValue;
     ar>>strValue;
     ar.Close();
     file.Close();
     //输入读到的数据
     printf("%d\n",nNum);
     printf("%.2f\n",fNum);
     printf("%s\n",strValue);


    }
    int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
    {

        Store();
     Load();
     return 0;
    }

    一 文件操作
       1 文件类相关
        CFile类-父类是COjbect,完成文件的打开和读写操作
        CFileFind类-父类是COjbect,提供了文件查找功能
       2 CFile类的使用
         2.1 打开或者创建文件
            CFile::Open
         2.2 数据读写
            CFile::Read/Write,注意读写操作的异常捕获
         2.3 文件的关闭
            CFile::Close
         2.4 文件指针位置
            CFile::Seek/SeekToBegin/SeekToEnd
         2.5 Flush-强制将缓冲区数据写入到硬盘
         2.6 LockRange/UnLockRange
             当多个程序同时操作同一文件时使用
         2.7 文件状态信息
             GetStatus/SetStatus
       3 CFileFind类
         3.1 FindFile-开始查找
         3.2 FindNextFile-查找下一个文件(得到第一个文件信息,
                          返回下一个文件是否存在)
         3.3 调用一系列的GetXXX和IsXXX函数获取和判断文件的信息
         3.4 Close-结束查找
              
    二 序列化
       1 序列化概念
         采用数据流的方式,将数据依次写入/读取文件。是二进制的存储方式    2 序列化相关类
         2.1 CFile-文件类
         2.2 CArchive-归档类,封装了序列化的操作(完成数据的具体读写操        作)
         2.3 CObject类(与对象的序列化相关的)
       3 序列化的使用    
         3.1 打开或者创建文件
         3.2 定义CArchive对象
         3.3 数据读写
             写,存储,<<
             读,加载,>>
         3.4 关闭CArchive对象
         3.5 关闭文件
       4 CArchive类
         CArchive中定义了缓存,目的减少硬盘的读写次数
         class CArchive
         {
           ...
            CFile* m_pFile;//关联的文件对象
            int m_nBufSize;//缓存的大小
     
     BYTE* m_lpBufCur;//当前指针位置
     BYTE* m_lpBufMax;//终止指针位置
     BYTE* m_lpBufStart;//起始指针位置
           ...
         }
                                     4094  +int
                                  m_lpBufCur
            100  12.25...              |
           |------------------------------|  (4096)
       m_lpBufStart                   m_lpBufMax
       
         4.1数据写入的执行过程(存储)
          4.1.1 判断在Buff中,是否有足够的空间存储数据
              m_lpBufCur+sizeof(数据)>m_lpBufMax
          4.1.2 如果空间不够,首先将Buff中已有的数据写入文件(Flush)            重置m_lpBufCur到Buff的起始位置
          4.1.3 如果空间足够,将数据写入m_lpBufCur执行的地址
          4.1.4 将m_lpBufCur向后偏移写入的字节数
          写入到最后,ar.Close()调用Flush
         4.2 数据读取的执行过程(加载)
       m_lpBufCur
           |
           |----------------------------AA|  (4096)
       m_lpBufStart                   m_lpBufMax
          
          4.2.1 判断在Buff中剩余的空间是否满足读取数据的大小
              m_lpBufCur+sizeof(数据)>m_lpBufMax 
                    
         m_lpBufCur
           |                           
           |AAAA--------------------------|  (4096)
       m_lpBufStart                   m_lpBufMax

      5 对象的序列化(6)
        序列化对象-将对象的类的信息和对象的数据保存到文件
        反序列化对象-根据读取的类的信息,创建对象,并将对象的数据读取
        class CStudent
        {
            CString m_strName;
            UINT m_nAge;
         }
         CStudent stu1("张三",18);
         5.1 定义一个支持序列化的类
             5.1.1 必须是CObject类的子类
             5.1.2 添加序列化的声明宏和实现宏
             5.1.3 重写CObject类的Serialize函数
         5.2 宏替换后的代码
           5.2.1 作用
              _init_CStudent-将CStudent类的运行时类信息的地址保存到          应用程序的m_classList链表中
             operator>>-创建类的对象并读取对象的成员变量数据
         5.3 序列化对象的过程
            5.3.1 获取对象的运行时类信息
            5.3.2 将类的名称、版本等信息写入到文件
            5.3.3 调用对象的Serialize函数
            5.3.4 在函数中,保存对象的成员变量的数据
            WriteObject->WriteClass->Store->Serialize
         5.4 反序列化对象的过程
            5.4.1 从文件中读取类的名称和版本号等信息
            5.4.2 根据读取到的类名称,在m_classList链表中查找该类所对        应的运行时类信息
            5.4.3 使用查询到的运行时类信息创建对象
            5.4.4 调用对象的Serialize函数
            5.4.5 在函数中,读取变量的数据
            ReadObject->ReadClass->Load->CreateObject->Serialize

  • 相关阅读:
    Nodejs与ES6系列3:generator对象
    Nodejs与ES6系列2:Promise对象
    Nodejs与ES6系列1:变量声明
    Nodejs与ES6系列4:ES6中的类
    angular单元测试与自动化UI测试实践
    javascript 异步模块加载 简易实现
    javascript模式 (3)——工厂模式和装饰模式
    TP手册学习第四内置天
    TP手册学习第三天
    tp5命令行基础
  • 原文地址:https://www.cnblogs.com/boobuy/p/2823138.html
Copyright © 2011-2022 走看看