Do not store data without the Document in the VC

zhaozj2021-02-16  52

Do not store data without the Document in the VC

(This article is only suitable for beginners to read, if you are a master, and you have to look, then it is a pastime after the meal. :-))

When I first started learning VC, I have been dizziness that I have been in a document / view structure.

Really, I still remember the first time using serial output, ar << m_xxx; this thing, how did I

I don't understand, it is how it will be. Later, I know that the carchive is overloaded << and >> two operators.

The role is, the data in the object is permanently saved on the disk (say jokes, it is permanent is relative here if you

Nothing is the same as the disk formatted). But these are generally rewritten in the derived class of CDocument classes.

The Serialize function is complete. This is very natural, because cdocument can be understood as an abstract form of a file

And View is the data provided by Document to complete the display of the user. But think about it. If, I am

If you are a blind man one day, or if you don't want to see all the data, if you still, those who want to display and don't display

Are the data in the cDocument class? The structure is obviously a bit chaotic. Yes, cdocument is complete because

Interacting with CView must be used. So well, although not to see those data, but must

Saved data, what should we do? Yes, no one wants, must use the serialize otherwise nothing

According to MSDN, you must do it from the COBJECT class to be derived from the COBJECT class. What else thinks?

Of course, it is a SaveData and a loadData. To know that we just don't want users to intervene, not these

Data, that is, these data is just not to deal with View. So well, I still come to some examples, which will be more convincing.

I am in CSERDATA.H first.

Class Cserdata: Public Cobject {Declare_Serial (Cserveata) // Don't forget that this macro can't do anything (recommended reference << Deeply shallow MFC >>)

PUBLIC:

Virtual void serialize; int m_nid; cstring m_strname; bool m_issave = false; savedata (); loadingdata ();

I saw it, I have to rewrite the virtual function. I don't have any of our own things? I have added it.

Two data to store, an int and a cstring, you can imagine it, a uniquely labeled ID and one

Can be the same name. You can even imagine it, a blacklist, a list of the list of users who will annoy

But you won't be stupid to see those users, otherwise he will modify these data. Of course this is a bit.

Let's implement the CPP file.

#include "stdafx.h" #include "caserdata.h"

Implement_serial (Cinfo, CObject, 1)

Void cinfo :: serialize (carchive & ar) {if (ar.d) // if you are loading data from the disk {ar >> m_strname; ar >> m_nid;} else // if you are storing data to the Disk {ar << m_strname; ar << m_nid;}}

Just like you often do it, just ar << m_xxxx; and in CDocument, of course, this is necessary.

Next, see what we have to do? Don't forget, if you don't write them to the file, don't even want to know

Those users entered a blacklist. Remember this file only when you play. Ok, remember, I am in CSerdata.h.

Savedata (); and loadData (); I feel that true OO should write within the class)

If you don't want to do it, you can write outside, I think as long as you like, the compiler doesn't get angry, no one is taking you.

#define data_file_t ("datatest.dat")

Void savedata () // code to serialize and save the data {uint nflags = cfile :: typebinary | cfile :: modewrite

IF (_ACCESS (Data_File, 0)) {nflags | = cfile :: modecreate; // the file doesn't exist, so create it m_bcansave = true;} else {int ntemp = _Access (data_file, 6); // check Read Write Permissions

IF (ntemp == 0) m_bcansave = true;

IF (m_bcansave) {cfile file; cfileException fe;

// The file exists with read & write permissions if (file.Open (DATA_FILE, nFlags, & fe)) {CArchive ar (& file, CArchive :: store); UpdateData (TRUE); CSerData.m_nID = m_nID; CSerData.m_strName = M_StrName; Cserveata.Serialize (ar); // serialize the data}}}

The above SAVE operation has ended, I think it is to LOAD.

Void loadingData () // Code to Load Data

{IF (_ACCESS (Data_File, 6) == 0) // if File; CFILEEXCEPTION FE;

IF (file.open (data_file, cfile :: typebinary | cfile :: ModeRead, & fe) {CARCHIVE AR (& File, Carchive :: Load); info.serialize (ar);}

}

m_nid = cserdata.m_nid; m_strname = cserdata.m_strname; Updatedata (false);}

Finally, I hope it is not so bad. I hope to be a bit of people. I said above, this article is only suitable for beginners.

If you are a master, I read it here. I have already explained that you are very idle today, or you are looking for the pastime after some tea.

Ok, my article hopes to bring you so much, see it next time.

转载请注明原文地址:https://www.9cbs.com/read-27447.html

New Post(0)