孙鑫VC++视频教程笔记(13)文档与串行化

langchain4j+milvus实战 本文主要研究一下如何使用langchain4j来对接向量数据库milvus。 阅读详情

把文件在磁盘上保存下来 叫做串行化
可以使用CArchive:
写入:
 CFile file("1.txt",CFile::modeCreate | CFile::modeWrite);
 CArchive ar(&file,CArchive::store);
 int i=4;
 float b=1.3f;   //C默认用float定义而不加f的为double型
 CString str="SongPeng";
 ar<<i<<b<<str;
读取:
 CFile file("1.txt",CFile::modeRead);
 CArchive ar(&file,CArchive::load);
 int i;
 float b;
 CString str;
 CString strRestult;
 ar>>i>>b>>str;
 strRestult.Format("%d %f %s",i,b,str);
 MessageBox(strRestult);
在BOOL CGraphicDoc::OnNewDocument()中,我们可以设置文档,例如:
 SetTitle("SongPeng")
 
OnNewDocument()用来在程序运行之初构造文档,或是新建文档:
Called by the framework as part of the File New command. The default implementation of this function calls the DeleteContents member function to ensure that the document is empty and then marks the new document as clean. Override this function to initialize the data structure for a new document. You should call the base class version of this function from your override.

我们已可以在String Table中修改标题,其中的IDR_MAINFRAME中的字串原为:
Graphic/n/nGraphi/n/n/nGraphic.Document/nGraphi Document
各子串之间用/n分割,可以有空子串。
各子串定义如下:
CDocTemplate::windowTitle   Name that appears in the application window’s title bar (for example, “Microsoft Excel”). Present only in the document template for SDI applications.


CDocTemplate::docName   Root for the default document name (for example, “Sheet”). This root, plus a number, is used for the default name of a new document of this type whenever the user chooses the New command from the File menu (for example, “Sheet1” or “Sheet2”). If not specified, “Untitled” is used as the default.


CDocTemplate::fileNewName   Name of this document type. If the application supports more than one type of document, this string is displayed in the File New dialog box (for example, “Worksheet”). If not specified, the document type is inaccessible using the File New command.


CDocTemplate::filterName   Description of the document type and a wildcard filter matching documents of this type. This string is displayed in the List Files Of Type drop-down list in the File Open dialog box (for example, “Worksheets (*.xls)”). If not specified, the document type is inaccessible using the File Open command.


CDocTemplate::filterExt   Extension for documents of this type (for example, “.xls”). If not specified, the document type is inaccessible using the File Open command.


CDocTemplate::regFileTypeId   Identifier for the document type to be stored in the registration database maintained by Windows. This string is for internal use only (for example, “ExcelWorksheet”). If not specified, the document type cannot be registered with the Windows File Manager.


CDocTemplate::regFileTypeName   Name of the document type to be stored in the registration database. This string may be displayed in dialog boxes of applications that access the registration database (for example, “Microsoft Excel Worksheet”).

在前两个/n之间加入字符串,就达到修改标题的效果。

在新建工程的第四部advance选项中也能进行修改

凡是从CCmdTarget派生出的类,都可以间接接受命令消息

新建文档运行路径:
void CWinApp::OnFileNew()--->void CDocManager::OnFileNew()---> 
CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetHead();--->
pTemplate->OpenDocumentFile(NULL);--->CDocument*
CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
 BOOL bMakeVisible)--->pDocument = CreateNewDocument();
--->pFrame = CreateNewFrame(pDocument, NULL);--->
if (!pDocument->OnNewDocument())--->BOOL CGraphicDoc::OnNewDocument()

CWinApp负责管理文档管理器

打开文档运行路径
void CWinApp::OnFileOpen()--->void CDocManager::OnFileOpen()--->
DoPromptFileName--->BOOL CDocManager::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate)--->打开一个文件--->AfxGetApp()->OpenDocumentFile(newName);--->
CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)--->
CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName)--->
match = pTemplate->MatchDocType(szPath, pOpenDocument);--->
判断pOpenDocument是否为空,pOpenDocument用来指定是否与先前的文档关联,即获取先前相同文档的指针,如果是,不再对文档进行操作---->
CDocument* CSingleDocTemplate::OpenDocumentFile(LPCTSTR lpszPathName,
 BOOL bMakeVisible)--->if (pDocument == NULL)--->if (pFrame == NULL)--->
if (lpszPathName == NULL)--->if (!pDocument->OnOpenDocument(lpszPathName))--->
BOOL CDocument::OnOpenDocument(LPCTSTR lpszPathName)---->
Serialize(loadArchive);---->void CGraphicDoc::Serialize(CArchive& ar)

生成可串行化的类:
 A serializable class usually has a Serialize member function, and it usually uses the DECLARE_SERIAL and IMPLEMENT_SERIAL macros, as described under class CObject.
分为五步:
1 . Deriving your class from CObject (or from some class derived from CObject).
2 . Overriding the Serialize member function.
3 . Using the DECLARE_SERIAL macro in the class declaration. 
4 . Defining a constructor that takes no arguments.
5 . Using the IMPLEMENT_SERIAL macro in the implementation file for your class.
IMPLEMENT_SERIAL(CGraph,CObject,1),1为版本号,保存和读取时版本号必须相同
一个文档类对象能和多个视类对象相关,一个视类对象只和一个文档类对象相关.

单文档类只有一个视类对象,要获取视类对象的指针
要在文档类中访问定义在视类中的对象,首先要获得视类的指针.需要用到函数
CDocument::GetFirstViewPosition 得到视类对象的位置
virtual POSITION GetFirstViewPosition( ) const;
A POSITION value that can be used for iteration with the GetNextView member function.
Call this function to get the position of the first view in the list of views associated with the document.
然后再调用GetNextView得到视类对象的指针
 POSITION pos=GetFirstViewPosition();
 CGraphicView *pView=(CGraphicView *)GetNextView(pos);

POSITION   A value used to denote the position of an element in a collection; used by MFC collection classes.

我们文档类中调用Serialize保存一个可串行化的类的数据时实际上是利用了这个对象本身的Serialize函数,这个对象需要什么对象,都需要在你编写可串行化的类时去确定

void CGraph::Serialize(CArchive &ar)
{
 if(ar.IsStoring())
 {
  ar<<m_nDrawType<<m_pOrigin<<m_pEnd;
 }
 else
 {
  ar>>m_nDrawType>>m_pOrigin>>m_pEnd;
 }
}

void CGraphicDoc::Serialize(CArchive& ar)
{
 POSITION pos=GetFirstViewPosition();
 CGraphicView *pView=(CGraphicView *)GetNextView(pos);
 if (ar.IsStoring())
{
  int nCount=pView->m_obArray.GetSize();
  ar<<nCount;
  for(int i=0;i<nCount;i++)
  {
   ar<<pView->m_obArray.GetAt(i);
  }
 }
 else
 {
  int nCount;
  ar>>nCount;
  CGraph *pGraph;
  for(int i=0;i<nCount;i++)
  {
   ar>>pGraph;
   pView->m_obArray.Add(pGraph);
  }
 }
}

数组的保存:
 CGraph *pGraph=new CGraph(m_nDrawType,m_pOrigin,point);
 m_obArray.Add(pGraph);

CObArray incorporates the IMPLEMENT_SERIAL macro to support serialization and dumping of its elements. If an array of CObject pointers is stored to an archive, either with the overloaded insertion operator or with the Serialize member function, each CObject element is, in turn, serialized along with its array index
void CGraphicDoc::Serialize(CArchive& ar)可被修改为:
void CGraphicDoc::Serialize(CArchive& ar)
{
 POSITION pos=GetFirstViewPosition();
 CGraphicView *pView=(CGraphicView *)GetNextView(pos);
 if (ar.IsStoring())
{
}
else
{
}
pView->m_obArrary.Serialize(ar);
}

以下是CObArray在MFC中的源代码:
void CObArray::Serialize(CArchive& ar)
{
 ASSERT_VALID(this);

 CObject::Serialize(ar);

 if (ar.IsStoring())
 {
  ar.WriteCount(m_nSize);
  for (int i = 0; i < m_nSize; i++)
   ar << m_pData[i];
 }
 else
 {
  DWORD nOldSize = ar.ReadCount();
  SetSize(nOldSize);
  for (int i = 0; i < m_nSize; i++)
   ar >> m_pData[i];
 }
}

文档类时用来管理数据的我们把CObArray m_obArray放入文档类中
以下为自动生成类,这是在View类中准备好的可以获得文档来指针的函数:
CGraphicDoc* CGraphicView::GetDocument() // non-debug version is inline
{
 ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicDoc)));
 return (CGraphicDoc*)m_pDocument;
}

 在View中可以这样调用m_obArray
CGraphicDoc *pDoc=GetDocument();
 pDoc->m_obArray.Add(pGraph);

Document/View结构
在MFC中,文档类负责管理数据,提供保存和数据加载的功能。视类负责数据的显示,以及给用户提供对数据的编辑和修改功能。
MFC给我们提供Document/View结构,将一个应用程序所需要的数据处理与显示的函数的空壳都设计好了,这些函数都是需函数,我们可以在派生类中重写这些函数。有关文件读写的操作在CDocument的Serialize函数中进行,有关数据和图形显示的操作在CView的OnDraw函数中进行。我们在其派生类中,只需要去关注serialize和OnDraw函数就可以了,其他的细节我们不需要去理会,程序就可以良好的运行。
当我么按下"File/Open",Application Framework会激活文件打开对话框,让你指定文件名,然后自动调用CGraphicView::OnDraw,传递一个显示DC,让你重新绘制窗口内容。
MFC给我们提供Document/View结构,是希望我们将精力放在数据结构的设计和数据显示的操作上,而不是把时间和精力花费在对象与对象之间,模块与模块之间的通信上。
一个文档对象可以和多个视类对象相关联,而一个视类对象只能和一个文档类对象相关联

不管是新建文档还是打开另外一份文档,都会调用DeletContents,以保证新建文档时空的

释放对内存
void CGraphicDoc::DeleteContents()
{
 // TODO: Add your specialized code here and/or call the base class
 int nCount;
 nCount=m_obArray.GetSize();
 for(int i=0;i<nCount;i++)
 {
  delete m_obArray.GetAt(i);  //解除指针与值之间的联系,但没有清除堆内存
  m_obArray.RemoveAt(i);  //清除堆内存
 }
 CDocument::DeleteContents();
}
CDocument::DeleteContents()会在打开,新建,关闭文档时被调用

CObArray::RemoveAt
Removes one or more elements starting at a specified index in an array. In the process, it shifts down all the elements above the removed element(s). It decrements the upper bound of the array but does not free memory.RemoveAt会导致数组中数据的重排
因此,需要把以上代码修改为
void CGraphicDoc::DeleteContents()
{
 // TODO: Add your specialized code here and/or call the base class
 int nCount;
 nCount=m_obArray.GetSize();
 for(int i=0;i<nCount;i++)
 {
  delete m_obArray.GetAt(i);
 }
 m_obArray.RemoveAll();
 CDocument::DeleteContents();
}
另一种方法
 while(nCount--)
 {
  delete m_obArray.GetAt(nCount);
  m_obArray.RemoveAt(nCount);
 }

【系统架构设计师-2023年真题】综合知识-答案及详解 要实现多任务间的协同工作,操作系统必须提供任务间的通信手段。嵌入式操作系统一般都会提供多任务间通信的方法,其中()是任务间最直接、最明显的通信方法,也是访问共享的数据结构,即不同的任务都可以访问同一地址空间。()作为一种更高级的通信方式,能够在同一处理器的各个任务间传递任意长度(理论上只受物理内存和机器字长限制)的信息。(1)A. 共享内存 B. Socket C. 消息传递 D. 信号量(2)A. 共享内存 B. Socket C. 消息传递 D. 信号量【答案】A、C关于星型拓扑结构,下列说法正确的是( 阅读详情

相关推荐

时间同步中 TOD 与GPRMC和IRIG-B的区别

时间同步中 TOD 与GPRMC和IRIG-B的区别

ftdlk的博客 1736

VC++技术内幕(第四版)笔记(第15章)

/********************************/第十五章:文档与视图的分离1,SDI应用程序文档类由CDocument类派生,一个文档类可以有一个或多个由CView类派生的视图类。 2,重要成员函数:1)CView::GetDocumentCDocument* GetDocument( ) const;//文档对象是用来保存数据的,而视图对象则是用来显示数据的。//一个视图对象

hbyufan的专栏 1万+

中国传媒大学计算机考研初复试经验贴

中传的计算机专业考研的话主要分为两大学院:计网学院和国重,大部分专业在计网学院。

DoKiDoK的博客 1575

从double到float的强制类型转换

#include &amp;amp;amp;amp;amp;lt;stdio.h&amp;amp;amp;amp;amp;gt; union FLOATUN{ unsigned int a; float b; }; int main(){ FLOATUN fun; fun.b = 12.34; printf(&amp;amp;amp;amp;quot;c:%x\n&amp;amp;amp;amp;quot;,fun.a); doubl

qianshishangdi的博客 3万+

故障排查:OpenClaw 更新后 feishu 插件路径失效的解决方法

OpenClaw 更新后 feishu 插件路径失效的解决方法发现问题→ Gateway 启动失败,报插件路径错误定位原因→ OpenClaw 目录结构变化,→解决问题→ 更新为正确的路径验证修复→ 使用确认一切正常。

刘一说的IT专栏 1313

3D结构光 vs TOF——手机厂商为何选择TOF方案?

3D视觉感知技术路线分析:结构光与TOF对比 3D视觉感知领域存在结构光和TOF两大主流技术。结构光基于三角定位法,精度高(毫米级)但工作距离短(<1.5米),适合人脸识别等近距离场景;TOF通过测量光脉冲飞行时间计算深度,精度中等(厘米级)但工作距离长(0.5-5米+),更适合AR导航等中远距离应用。手机厂商选型时考虑:前置认证选择结构光(如苹果FaceID),后置场景倾向TOF(安卓阵营),因后者更适配全面屏设计、环境适应性强且成本优势明显。两种技术互补共存,共同推动3D感知应用发展。

致力于lidar应用开发 121

VC++更改标题

一、单文档接口 (SDI) 类型窗口标题的改变 首先,我们来看Windows SDI类型窗口标题栏的结构形式。结合具体例子说明:用 VC 的 AppWizard 创建一个名为“SDITitle”的单文档类型的工程,创建完工程后,编译运行该工程,程序主窗口标题形式如下: 文档标题 主窗口标题 1、文档标题的改变 文档标题在是由工程中相应的文档类所控制的,因此我们可以利用SetTitle()函...

netstore的专栏 1255

【MATLAB-app】csdn上第一套关于matlab appdesigner界面设计 系列“视频课”来啦~~

写作时间: 2020-12-10 标题: 01_为什么推荐你学习appdesigner? 目录: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) 正文: 1.什么是appdesigner? 2.appdesigner能干什么,有什么好处? 3.怎么学习?保证你一周内能够学会! 4.我的课程总目录 5.重磅来袭(如何获取学习资料代码+文档+视频) THE END

wind 2万+

孙鑫VC视频教程笔记之第十三课“文档与串行化”

What is the serialization?The CArchive class allows you to save a complex network of objects in a permanent binary form (usually disk storage) that persists after those objects are deleted. Later yo

Roger Ge 的专栏 1869

LDO 上电过冲的根因

LDO在上电瞬间常出现短时过冲电压(3.4V-3.5V),可能损坏敏感器件。其根本原因是:低

硬核科技 312

【异常】Cursor IDE 扩展主机超时错误处理文档The agent execution provider did not respond within 4 seconds.

该错误通常是 Cursor IDE 的临时性问题,不影响项目代码。大多数情况下,重新加载窗口或重启 IDE 即可解决。如果问题持续,可按上述进阶方案排查。重新加载窗口(最快)重启 IDE检查系统资源清理缓存检查扩展更多关于 AI+教育领域的干货,可以关注我的微信公众号第一时间获取哦~

奶爸的编程之路,也就一周冷个三天 2729

GPS坐标变换时不要将double强制转换成float类型,否则会造成精度不够

        GPS坐标变换时不要将double强制转换成float类型,否则会造成精度不够,强制转换时会导致,精度下降,进而导致无法用于控制和跟踪,第一张图为经纬度由double类型强制转换成float类型,导致根本不在一条直线上.第二张是不进行强制转换的结果,可以都在一条直线上....

Mr_Yoky的博客 1446

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

c语言中的double如何转换为,c语言中double如何转换成float类型

c语言中double如何转换成float类型0#include#include#include#define max_dimension 200int m;int n;static double c[max_dimension][max_dimension];static double b[max_dimension][max_dimension];static double a[max_dime...

weixin_30194213的博客 4623

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

怎样将double强制转换为float

标记:iphone开发 直接这样: float x = [self aComplexMethodToGetADoubleValue]; 有时会出现莫名其妙的结果,最可能的原因就是溢出。 如何解决反向转换? 使用NSNumber类 double doubleX = [self aComplexMethodToGetADoubleValue]; NSNumber number = [NSN

南宫辞迁 7445

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

C语言的int, float,double相互转化(从本质上理解可能的问题)

从学了C语言之后,一直习惯于C/C++任意的强制转化,但是C语言的强制转化却总是带来意想不到的后果,在这里,我将从int,float,double的本质上讲解这些可能出现的问题以及解决办法,在下面你将看到: OK,现在好戏开始。 int unsigned int: unsigned int所进行的是模数计算,就是正常的二进制相加减,计算方法和十进制加减并无区别,但是unsigned

写代码的柯长的博客 5万+

double和float转换的问题

这是一个一分钱引发的血案。案情现场还原 float f = 280.8f; System.out.println(f*100); 结果是什么? 结果是:28080.0f(我是这么想的) 实际结果是:28079.998 既然float处理有问题换double会不会有问题呢? double f = 280.8d; System.out.println(f*100); 结果是:

憨厚博客 2万+

孙鑫VC++第13章文档和串行化

第13章  文档与串行化   1.      使用CArchive类对文件进行读写操作   CArchive类对象将对象数据保存到永久设备上,例如磁盘文件,让对象数据持久性的过程就称之为串行化(活序列化);     在创建CArchive对象之前必须先创建一个CFile类或者其派生类对象,并且因此存档对象既可以用来加载数据,也可以用来保存数据,所以确保这个CFile类对象的打开方式与

C__Monkey的专栏 1646

孙鑫 第十三课之一 文档与串行化(CArchive类保存文件)

用CArchive类保存/打开文件CArchive(CFile*   pFile,//对应的文件指针INT   nMode,// 模式,取值:CArchive::load, CArchive::store, CArchive::bNoFlushOnDelete   int    nBufSize=4096, //默认buffer大小void*   lpBuf=NULL //如果默认则会自动释放该b

cuihao0532的博客 458

孙鑫 第十三课之二 文档与串行化( IDR_MAINFRAME)

IDR_MAINFRAME各个参数的意义CSingleDocTemplate类定义了一个文档模板用于实现单文档界面(SDI)。一个SDI应用程序使用主框架窗口来显示一个文档;一次只能打开一个文档。IDR_MAINFRAME是CSingleDocTemplate的一个重要参数。(1)CDocTemplate::windowTitle 主窗口标题栏上的字符串,MDI程序不需要指定 (2)CDocTem

cuihao0532的博客 539

CObArray 源码解析

(1)CObArray 简介            CObArray类支持CObject指针数组。这些对象数组近似于C中的数组,但不同的是它们可以动态地增减。 (2)成员函数 构造函数 CObArray 为CObject指针构造空数组 约束 GetSize 获取数组中的元素数

q_023的博客 1130
上一篇: 孙鑫VC++视频教程(12)文件操作
下一篇: 孙鑫C++视频笔记(14)网络编程
peng6662001
博客等级 码龄21年 57粉丝 39原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值