Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2012851
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2015-10-23 19:51:13

c文件

点击(此处)折叠或打开

  1. #include "CfgParam.h"

  2. CCfgParam::CCfgParam()
  3. {

  4. }
  5. CCfgParam::~CCfgParam()
  6. {
  7. }

  8. CCfgParam& CCfgParam::operator=(const CCfgParam& rhs)
  9. {
  10.     

  11.     //strParameter = rhs.strParameter;
  12.     
  13.     com = rhs.com;

  14.     graph = rhs.graph;

  15.     return *this;
  16. }


  17. IMPLEMENT_SERIAL(CCfgParam,CObject,0)

  18. #define SIZEOF_MAXBUF    260
  19. void CCfgParam::Serialize(CArchive& ar)
  20. {
  21.     CObject::Serialize(ar);
  22.     BYTE tmp[SIZEOF_MAXBUF];
  23.     memset(tmp, 0, SIZEOF_MAXBUF);
  24.     if (ar.IsStoring())///write
  25.     {
  26.         //strParameter
  27.         int i;
  28.         memcpy(tmp, &com, sizeof(com));
  29.         for (i = 0; i < SIZEOF_MAXBUF; i ++)
  30.         {
  31.             ar << tmp[i];
  32.         }

  33.         memcpy(tmp, &graph, sizeof(graph));
  34.         for (i = 0; i < SIZEOF_MAXBUF; i ++)
  35.         {
  36.             ar << tmp[i];
  37.         }

  38.         memcpy(tmp, &flag, sizeof(flag));
  39.         for (i = 0; i < SIZEOF_MAXBUF; i ++)
  40.         {
  41.             ar << tmp[i];
  42.         }
  43.         
  44.         //ar << dwFlag;
  45.         ///ar << m_strIP << m_uiPort;
  46.     }
  47.     else//read
  48.     {
  49.         int i;
  50.         //strParameter

  51.         for (i = 0; i < SIZEOF_MAXBUF; i ++)
  52.         {
  53.             ar >> tmp[i];
  54.         }
  55.         memcpy(&com, tmp, sizeof(com));


  56.         for (i = 0; i < SIZEOF_MAXBUF; i ++)
  57.         {
  58.             ar >> tmp[i];
  59.         }
  60.         memcpy(&graph, tmp, sizeof(graph));



  61.         for (i = 0; i < SIZEOF_MAXBUF; i ++)
  62.         {
  63.             ar >> tmp[i];
  64.         }
  65.         memcpy(&flag, tmp, sizeof(flag));

  66.         //ar >> m_strIP >> m_uiPort;
  67.         //ar >> dwFlag;
  68.     }
  69. }
h文件(公共类的定义

点击(此处)折叠或打开

  1. #include "StdAfx.h"


  2. typedef struct
  3. {
  4.     BYTE byPort;
  5.     DWORD dwBaud;
  6. }STRCOM;

  7. typedef struct
  8. {
  9.     float f_x0;
  10.     float f_x1;
  11.     float f_y0;
  12.     float f_y1;
  13. }STRNETGRAPH;


  14. typedef struct
  15. {
  16.     STRCOM com;
  17.     STRNETGRAPH netgraph;
  18. }STRCFGPARAM;

  19. typedef struct
  20. {
  21.     BOOL bWriteCsvFile;
  22.     
  23. }STRBOOLCFG;



  24. class CCfgParam:public CObject
  25. {
  26. public:
  27.      CCfgParam();
  28.     ~CCfgParam();

  29.     DWORD dwFlag;

  30.     DECLARE_SERIAL(CCfgParam);



  31.     CCfgParam& CCfgParam::operator=(const CCfgParam&);

  32.     virtual void Serialize(CArchive& ar);

  33.     //STRCFGPARAM strParameter;
  34.     STRCOM com;
  35.     STRNETGRAPH graph;
  36.     STRBOOLCFG flag;

  37. protected:
  38. private:
  39. };
应用:

点击(此处)折叠或打开

  1. BOOL CVc_netGraphDlg::SaveParam(CCfgParam *param)
  2. {
  3.     //将数据写入文件
  4.     if (param == NULL)
  5.         return FALSE;

  6.     CString strFileName;
  7.     
  8.     strFileName="xxx";
  9.     strFileName= strFileName +".cfg";

  10.     CFileDialog fdlg(FALSE, ".cfg", strFileName ,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,

  11.         "*.CFG(CFG 文件)|*.CFG|*.*(所有文件)|*.*||");

  12.     //if (fdlg.DoModal() == IDOK)
  13.     //{
  14.         CString filePath = fdlg.GetPathName();
  15.         CFile file;
  16.         CFileException fe;

  17.         //filePath += "\\" + strFileName + ".cfg";
  18.         //以读方式打开文件
  19.         if(!file.Open(filePath,CFile::modeWrite|CFile::modeCreate,&fe))
  20.         {
  21.             fe.ReportError();
  22.             return FALSE;
  23.         }    
  24.         //构建CArchive 对象
  25.         CArchive ar(&file,CArchive::store);
  26.         ar <<param;
  27.         ar.Flush();
  28.         //写完毕,关闭文件流
  29.         ar.Close();
  30.         file.Close();
  31.         //return TRUE;
  32.     //}

  33.     return FALSE;

  34. }

  35. BOOL CVc_netGraphDlg::LoadParam(CCfgParam *param)//test ok
  36. {
  37.     extern CVc_netGraphApp theApp;
  38.     
  39.     
  40.     CString strFileName ="xxx";


  41.     char szPath[128];
  42.     GetModuleFileName(theApp.m_hInstance, szPath, 128);
  43.     CString strPath=szPath;
  44.     int nPos = strPath.ReverseFind('\\');
  45.     CString strDir = strPath.Left(nPos);

  46.     strDir = strDir + "\\" + strFileName + ".cfg";

  47.     CFile file;
  48.     CFileException fe;
  49.     
  50.     try

  51.     {
  52.             if(!file.Open(strDir, CFile::modeRead, &fe))
  53.             {
  54.                 fe.ReportError();
  55.                 return FALSE;
  56.             }else
  57.             {
  58.                 CArchive ar(&file,CArchive::load);
  59.                 CCfgParam* p = new CCfgParam();//very important
  60.                 ar >> p;

  61.                 *param = *p;
  62.             
  63.                 delete p;

  64.                 //ar>>param;

  65.                 ar.Flush();
  66.                 ar.Close();
  67.                 file.Close();
  68.                 return TRUE;
  69.             }

  70.     }
  71.     catch(...)
  72.     {
  73.         //
  74.         AfxMessageBox("not find cfg file,load default config");
  75.     }


  76. }

  77. void CVc_netGraphDlg::OnBnClickedButton2()
  78. {
  79.     // TODO: 在此添加控件通知处理程序代码
  80.     CCfgParam q;

  81.     LoadParam(&q);
  82. }

阅读(740) | 评论(0) | 转发(0) |
0

上一篇:CSerial串口类

下一篇:自定义串口解析协议

给主人留下些什么吧!~~