📄 ch08.htm
字号:
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 8.9—</I>FILE2VIEW.CPP<I>—Editing the Data Strings</I></P>
<pre><font color="#008000">void CFile2View::OnEditChangemessages()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> // TODO: Add your command handler code here</font></pre>
<pre><font color="#008000"> CFile2Doc* pDoc = GetDocument();</font></pre>
<pre><font color="#008000"> </font></pre>
<pre><font color="#008000"> CChangeDialog dialog(this);</font></pre>
<pre><font color="#008000"> dialog.m_message1 = pDoc->m_messages.GetMessage(1);</font></pre>
<pre><font color="#008000"> dialog.m_message2 = pDoc->m_messages.GetMessage(2);</font></pre>
<pre><font color="#008000"> dialog.m_message3 = pDoc->m_messages.GetMessage(3);</font></pre>
<pre><font color="#008000"> int result = dialog.DoModal();</font></pre>
<pre><font color="#008000"> if (result == IDOK)</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> pDoc->m_messages.SetMessage(1, dialog.m_message1);</font></pre>
<pre><font color="#008000"> pDoc->m_messages.SetMessage(2, dialog.m_message2);</font></pre>
<pre><font color="#008000"> pDoc->m_messages.SetMessage(3, dialog.m_message3);</font></pre>
<pre><font color="#008000"> pDoc->SetModifiedFlag();</font></pre>
<pre><font color="#008000"> Invalidate();</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000">}</font></pre>
<P>The real action, however, happens in the document class's <font color="#008000">Serialize()</font> function, where the <font color="#008000">m_messages</font> data object is serialized out to disk. This is accomplished by calling the data object's own
<font color="#008000">Serialize()</font> function inside the document's <font color="#008000">Serialize()</font>, as shown in Listing 8.10.</P>
<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>
<P><I>Listing 8.10—</I>FILE2DOC.CPP<I>—Serializing the Data Object</I></P>
<pre><font color="#008000">void CFile2Doc::Serialize(CArchive& ar)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> m_messages.Serialize(ar);</font></pre>
<pre><font color="#008000"> if (ar.IsStoring())</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> // TODO: add storing code here</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> else</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> // TODO: add loading code here</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000">}</font></pre>
<P>As you can see, after serializing the <font color="#008000">m_messages</font> data object, there's not much left to do in the document class's <font color="#008000">Serialize()</font> function. Notice that the call to <font
color="#008000">m_messages.Serialize()</font> passes the archive object as its single parameter.</P>
<H3><A ID="I10" NAME="I10"><B>Reading and Writing Files Directly</B></A></H3>
<P>Although using MFC's built-in serialization capabilities is a handy way to save and load data, sometimes you need more control over the file-handling process. For example, you might need to deal with your files non-sequentially, something the <font
color="#008000">Serialize()</font> function and its associated <font color="#008000">CArchive</font> object can't handle because they do stream I/O. In this case, you can handle files almost exactly as they are handled by non-Windows programmers: creating,
reading, and writing files directly. Even when you need to dig down to this level of file handling, though, MFC offers help. Specifically, you can use the <font color="#008000">CFile</font> class and its derived classes to handle files directly.</P>
<P><A ID="I11" NAME="I11"><B>The File Demo 3 Application</B></A><B>: working with CFile</B></P>
<P>This book's CD-ROM contains an example program that shows how the <font color="#008000">CFile</font> class works. You can find this program in the <font color="#008000">CHAP9\FILE3</font> folder. When you run the program, you see the window shown in
Figure 8.6. By choosing <U>E</U>dit, <U>C</U>hange Message, you can edit the string that's displayed in the window (see Figure 8.7). Finally, you can save and load the displayed text string by choosing the <U>F</U>ile, <U>S</U>ave, and <U>F</U>ile,
<U>O</U>pen commands, respectively (see Figure 8.8). (File Demo 3 uses one hardcoded file name, as you'll see shortly.)</P>
<A HREF="Jfigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs06.gif"><b>Fig. 8.6</b></A>
<P><I>The File Demo 3 application uses the </I><I>CFile</I><I> class for direct file handling.</I></P>
<A HREF="Jfigs07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs07.gif"><b>Fig. 8.7</b></A>
<P><I>Use the Change Message dialog box to edit the application's display string.</I></P>
<A HREF="Jfigs08.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch08/Jfigs08.gif"><b>Fig. 8.8</b></A>
<P><I>The </I><I><U>F</U></I><I>ile menu enables you to save and load the application's display string.</I></P>
<P><A ID="I12" NAME="I12"><B>The CFile Class</B></A></P>
<P>MFC's <font color="#008000">CFile</font> class encapsulates all the functions you need to handle any type of file. Whether you want to perform common sequential data saving and loading or want to construct a random-access file, the <font
color="#008000">CFile</font> class gets you there. Using the <font color="#008000">CFile</font> class is a lot like handling files the old-fashioned C-style way, except the class hides some of the busy-work details from you so that you can get the job done
quickly and easily. For example, you can create a file for reading with only a single line of code. Table 8.1 shows the <font color="#008000">CFile</font> class's member functions and their descriptions.</P>
<P><I>Table 8.1—Member Functions of the </I>CFile<I> Class</I></P>
<TABLE BORDER>
<TR>
<TD>
<P><B>Function</B></P>
<TD>
<P><B>Description</B></P>
<TR>
<TD>
<pre><font color="#008000">Constructor</font></pre>
<TD>
<P>Creates the <font color="#008000">CFile</font> object. If passed a file name, it opens the file.</P>
<TR>
<TD>
<pre><font color="#008000">Destructor</font></pre>
<TD>
<P>Cleans up a <font color="#008000">CFile</font> object that is going out of scope. If the file is open, it closes that file.</P>
<TR>
<TD>
<pre><font color="#008000">Abort()</font></pre>
<TD>
<P>Immediately closes the<A ID="I13" NAME="I13"> file with no regard for errors.</A></P>
<TR>
<TD>
<pre><font color="#008000">Close()</font></pre>
<TD>
<P>Closes the file.</P>
<TR>
<TD>
<pre><font color="#008000">Duplicate()</font></pre>
<TD>
<P>Creates a duplicate file object.</P>
<TR>
<TD>
<pre><font color="#008000">Flush()</font></pre>
<TD>
<P>Flushes data from the stream.</P>
<TR>
<TD>
<pre><font color="#008000">GetFileName()</font></pre>
<TD>
<P>Gets the file's filename.</P>
<TR>
<TD>
<pre><font color="#008000">GetFilePath()</font></pre>
<TD>
<P>Gets the file's full path.</P>
<TR>
<TD>
<pre><font color="#008000">GetFileTitle()</font></pre>
<TD>
<P>Gets the file's title (the filename without the extension).</P>
<TR>
<TD>
<pre><font color="#008000">GetLength()</font></pre>
<TD>
<P>Gets the file's length.</P>
<TR>
<TD>
<pre><font color="#008000">GetPosition()</font></pre>
<TD>
<P>Gets the current position within the file.</P>
<TR>
<TD>
<pre><font color="#008000">GetStatus()</font></pre>
<TD>
<P>Gets the file's status.</P>
<TR>
<TD>
<pre><font color="#008000">LockRange()</font></pre>
<TD>
<P>Locks a portion of the file.</P>
<TR>
<TD>
<pre><font color="#008000">Open()</font></pre>
<TD>
<P>Opens the file.</P>
<TR>
<TD>
<pre><font color="#008000">Read()</font></pre>
<TD>
<P>Reads data from the file.</P>
<TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -