VC视频教程笔记!(12-13课)

作者在 2009-03-09 09:31:03 发布以下内容

第12课 文件操作
1.常量指针与指针常量的区分
     char ch[5]="lisi";
     const char *pStr=ch;//const在*之前,表明指针指向的内容为常量,即为常量指针
     char * const pStr=ch;//const在*之后,表明指针的地址不能改变,即为指针常量
     明白?
2.对文件读写的三种方法
     1.C中
       FILE *pFile=fopen("1.txt","w");
fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
//fseek(pFile,0,SEEK_SET);
//fwrite("ftp:",1,strlen("ftp:"),pFile);
//fwrite("http://www.sunxin.org",1,strlen("http://www.sunxin.org"),pFile);
fclose(pFile);*/
//fflush(pFile);
     2.C++中
/* ofstream ofs("4.txt");
ofs.write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
ofs.close();*/
            要包括头文件 "fstream.h"
     3.MFC中 用CFile类,哈哈!简单好用
CFileDialog fileDlg(FALSE);
fileDlg.m_ofn.lpstrTitle="我的文件保存对话框";
fileDlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
fileDlg.m_ofn.lpstrDefExt="txt";
if(IDOK==fileDlg.DoModal())
{
     CFile file(fileDlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
     file.Write("http://www.sunxin.org",strlen("http://www.sunxin.org"));
     file.Close();
}
     4.利用win32 API函数 CreateFile(),及WriteFile()
4.注册表读写
     1.对win.ini的读写
//::WriteProfileString("http://www.sunxin.org","admin","zhangsan");
/* CString str;
::GetProfileString("http://www.sunxin.org","admin","lisi",
     str.GetBuffer(100),100);
AfxMessageBox(str);*/
     2.注册表的读写
HKEY hKey;
DWORD dwAge=30;
RegCreateKey(HKEY_LOCAL_MACHINE,"Software\\http://www.sunxin.org\\admin",&hKey);
RegSetValue(hKey,NULL,REG_SZ,"zhangsan",strlen("zhangsan"));
RegSetValueEx(hKey,"age",0,REG_DWORD,(CONST BYTE*)&dwAge,4);
RegCloseKey(hKey);以上是写入
代码比较简单,不再详细介绍。本笔记也不是为介绍函数而存在的。嘿嘿

第13课 文档与串行化
1.CArchive在菜单打开保存时的代码
CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
CArchive ar(&file,CArchive::store);
int i=4;
char ch='a';
float f=1.3f;
CString str("http://www.sunxin.org");
ar<<i<<ch<<f<<str;以上是保存,打开略
2.文档-视类结构简介
      OnNewDocument在程序启动时被调用,此时可设置文档标题,也可以在String Table的IDR_MAINFRAME的第二个"\"后改变文档的标题。须了解的7个字符串的用途,见PPT。
      在WinAPP的InitInstance()中完成DOC,View,MainFrame的归一。
      当点击系统的打开和新建菜单时,有一系列的步骤,孙鑫老师给我们跟踪了代码的调用过程,此段跟踪我们略过。但我们要牢记住:CWinAPP负责管理文档管理器,文档管理器有一个指针链表,且来保存文档模板的指针,文档模板指针管理三个类DOC,VIEW,MAINFRAME,使其为某文件对象服务。
3.利用CArchive来保存一个类的对象,此类必须支持串行化,需要5个步骤。
     a.让类从CObject派生;
     b.覆盖Serialize()函数,在其中完成保存和读取功能;
     c.在.h中加入 DECLARE_SERIAL(CGraph);
     d.在。cpp中加入IMPLEMENT_SERIAL(CGraph, CObject, 1 );
     e.定义一个不带参数的构造函数。
保存绘画数据到文件的简单过程
     a.在CGraph中增加一个画图的成员函数,其实不增加也行。可以在View中完成相应功能。
     b.增加四个画图菜单,菜单可以从11课的代码中拷贝。
     c.在View中增加LButtonDown和UP的响应,在UP中画图,在DOWN中保存点
     d.利用CObArray集合类来保存绘画数据
     e.在CGraphicDOC::Serialize()中保存和读取数据
     f.然后在OnDraw中重绘。
4.新建和打开文档时,要注意销毁原来的数据。在DOC的DeleteContents虚函数中是好时机。代码如下
int nCount;
nCount=m_obArray.GetSize();
/*for(int i=0;i<nCount;i++)
{
     delete m_obArray.GetAt(i);//释放指针指向的内存空间
     //m_obArray.RemoveAt(i);//移除链表中的元素。嘿嘿,别搞错了。但在此处不能这样用,会导致非法操作。要用下面的方法沙
}
m_obArray.RemoveAll();*/
while(nCount--)
{
     delete m_obArray.GetAt(nCount);
     m_obArray.RemoveAt(nCount);
}

VC++ | 阅读 2221 次
文章评论,共0条
游客请输入验证码
浏览196729次