MFC summary (a): MFC file operations, locate and select the summary (2)

(B) operating the CArchive class

     CArchive class to read and write the buffer data while storing and reading program defines the class object. In the document view programming, and binding CList CArray like can easily achieve data read and write.

     (1) Basic creates display data read CArchive object ----

     A. Before use CArhive class CFile must be a file object, and must ensure CArchive CFile operation must be in an open state, and the operation does not change during file status.

     B. Then define CArchive object, associate it with CFile object and specify which mode is used to buffer read or write:

      Ar the CArchive ( pFile, // target file, CFile object pointer

                              nMode , // mode of operation, the read / write

                              nBufSize , // buffer size is specified, the default is 4096

                              lpBuf            // buffer pointer defaults to NULL, i.e. remove a heap memory, after the automatic release from the local

                            );

      C. For the basic data types, can be used directly operator >> and operator << for data reading and writing. These data type BYTE, WORD, LONG, DWORD, float, double, int, short, char, unsigned u like. For CString object can also be used.

       For custom data type structure, we ourselves can override these two operators, one example is as follows.

    typedef struct test_st
    {
         float valueX;
         float valueY;

         //重载<<和>>运算符
        friend CArchive& AFXAPI operator <<(CArchive& ar, const test_st& info)
      {
            //保存
           ar<<info.valueX<<info.valueY;
           return ar;
      }
       friend CArchive& AFXAPI operator >>(CArchive& ar, test_st& info)
       {
            //读取
            ar>>info.valueX>>info.valueY;
            return ar;
       }
    }TEST;

     D. Use Read and Write can specify the data length for data write / read, the data length here refers to the number of bytes.

         Use WriteString writable string, the ReadString read out a line of characters. Note that this is not a write line WriteString string is not written when write WriteString string '\ 0' is not automatically write '\ n-' . Here is an example from MSDN, I believe telling.

CFile myFile("myfile", CFile::modeCreate | CFile::modeReadWrite);
CString str1="String1", str2="String2", str;

// Create a storing archive.
CArchive arStore(&myFile, CArchive::store);

// Write str1 and str2 to the archive
arStore.WriteString( str1 );
arStore.WriteString( "\n" );
arStore.WriteString( str2 );
arStore.WriteString( "\n" );

// Close the storing archive
arStore.Close();

// Create a loading archive.
myFile.SeekToBegin();
CArchive arLoad(&myFile, CArchive::load);

// Verify the two strings are in the archive.
arLoad.ReadString( str );
ASSERT( str == str1 );
arLoad.ReadString( str );
ASSERT( str == str2 );

      E. IsLoading IsStoring function and is used to read or write is determined.

          Close function for association with a cutting CFile object, before this will automatically call the Flush the buffer data written in the storage medium.

     F. in the program, if not call the function Flush (), then the real writing data to the physical disk is turned off in the calling function Close (). Therefore, some important data need to use Flush () function to write files immediately, to prevent loss.

      Write (2) object class

       A. CArchive use save / load a class object, the class must support serialization.

      B. custom serialization class five steps:

          ① inherit CObject class;

          ② overloaded Serialize member function CObject class;

          ③ In the .h file class, serializing statement: DECLARE_SERIAL (class name);

          ④ define a constructor with no arguments;

          ⑤ In the .cpp file class, declared: IMPLEMENT_SERIAL (class name, CObject, version number)

      C. In Serialize member function custom, using procedure (1) above, save / load basic data types. as follows. In places where a file operation, the function can be called directly.

         void CXXXX::Serialize(CArchive &ar)
         {
              if (ar.IsStoring())
              {    //保存
                   //Add code....
              }
              else
              {   //读取

                 //Add code...
              }
         }

       D. With this method, may be implemented in a distributed data storage. Usually in the document view programming, we define data objects in the document class, and then implement the preservation of data / load may in its Serialize function.

We usually define a container used to store data. For example, in my project, I used the

CTypedPtrList<CObList, CObject*> m_DataList;

This is a list, which element is a pointer to CObject class object, when we customized support serialization of the class, you can put the data added to this list, it is very convenient for data management and storage.


     The last recommendation two URLs,

        http://hi.baidu.com/andywangcn/blog/item/f892b43fa42dc13270cf6cde.html which is an example, we can look at.

        http://hi.baidu.com/bin545/blog/item/1a6a58af96d164fdfaed5028.html inside source for CArchive member functions of the class to do some analysis, in-depth study.

     Unfinished, continued. . .



Published 37 original articles · won praise 204 · views 440 000 +

Guess you like

Origin blog.csdn.net/zwgdft/article/details/7107276