⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:

<TD>

<pre><font color="#008000">Remove()</font></pre>

<TD>

<P>Deletes a file.</P>

<TR>

<TD>

<pre><font color="#008000">Rename()</font></pre>

<TD>

<P>Renames the file.</P>

<TR>

<TD>

<pre><font color="#008000">Seek()</font></pre>

<TD>

<P>Sets the position within the file.</P>

<TR>

<TD>

<pre><font color="#008000">SeekToBegin()</font></pre>

<TD>

<P>Sets the position to the beginning of the file.</P>

<TR>

<TD>

<pre><font color="#008000">SeekToEnd()</font></pre>

<TD>

<P>Sets the position to the end of the file.</P>

<TR>

<TD>

<pre><font color="#008000">SetFilePath()</font></pre>

<TD>

<P>Sets the file's path.</P>

<TR>

<TD>

<pre><font color="#008000">SetLength()</font></pre>

<TD>

<P>Sets the file's length.</P>

<TR>

<TD>

<pre><font color="#008000">SetStatus()</font></pre>

<TD>

<P>Sets the file's status.</P>

<TR>

<TD>

<pre><font color="#008000">UnlockRange()</font></pre>

<TD>

<P>Unlocks a portion of the file.</P>

<TR>

<TD>

<pre><font color="#008000">Write()</font></pre>

<TD>

<P>Writes data to the file.</P></TABLE>

<P>As you can see from the table, the <font color="#008000">CFile</font> class offers up plenty of file-handling power. The File Demo 3 application demonstrates how to call a few of the <font color="#008000">CFile</font> class's member functions. However, 
most of the other functions are just as easy to use.</P>

<P><A ID="I14" NAME="I14"><B>Exploring the File Demo 3 Application</B></A></P>

<P>When the File Demo 3 application starts up, the program sets its display string to &quot;Default Message,&quot; sets the file path to &quot;None,&quot; and sets the file's length to 0. Whenever the user saves a document to a file, or opens a file, the 
view is updated with new file information. (For the sake of simplicity, all of the file handling is done in the view class.) When the user chooses the <U>E</U>dit, <U>C</U>hange Message command, the program displays the Change Message dialog box, which 
happens in the view class's <font color="#008000">OnEditChangemessage()</font> member function, shown in Listing 8.11.</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.11&#151;</I>FILE3VIEW.CPP<I>&#151;Changing the Display String</I></P>

<pre><font color="#008000">void CFile3View::OnEditChangemessage()</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">   </font></pre>

<pre><font color="#008000">    CChangeDlg dialog(this);</font></pre>

<pre><font color="#008000">    dialog.m_message = m_message;</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">        m_message = dialog.m_message;</font></pre>

<pre><font color="#008000">        Invalidate();</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">}</font></pre>

<P>In this function, the program displays the dialog box, and if the user exits the dialog box by clicking the OK button, the program sets the view class's <font color="#008000">m_message</font> data member to the string entered in the dialog box. A call 
to <font color="#008000">Invalidate()</font> ensures that the new string displays in the window. The process of displaying dialog boxes and extracting data from them was first covered in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, &quot;Dialog Boxes 
and Controls.&quot;</P>

<P>When the user chooses the <U>F</U>ile, <U>S</U>ave command, MFC calls the view class's <font color="#008000">OnFileSave()</font> member function, which is shown in Listing 8.12.</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.12&#151;</I>FILE3VIEW.CPP<I>&#151;The Application's </I>OnFileSave()<I> </I><I>Function</I></P>

<pre><font color="#008000">void CFile3View::OnFileSave()</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">    // Create the file.</font></pre>

<pre><font color="#008000">    CFile file(&quot;TESTFILE.TXT&quot;,</font></pre>

<pre><font color="#008000">        CFile::modeCreate | CFile::modeWrite);</font></pre>

<pre><font color="#008000">    // Write data to the file.</font></pre>

<pre><font color="#008000">    int length = m_message.GetLength();</font></pre>

<pre><font color="#008000">    file.Write((LPCTSTR)m_message, length);</font></pre>

<pre><font color="#008000">    // Obtain information about the file.</font></pre>

<pre><font color="#008000">    m_filePath = file.GetFilePath();</font></pre>

<pre><font color="#008000">    m_fileLength = file.GetLength();</font></pre>

<pre><font color="#008000">    // Repaint the window.</font></pre>

<pre><font color="#008000">Invalidate();</font></pre>

<pre><font color="#008000">}</font></pre>

<P>In <font color="#008000">OnFileSave()</font>, the program first creates the file, as well as sets the file's access mode, by calling the <font color="#008000">CFile</font> class's constructor. Notice that you do not have to explicitly open the file 
when you pass a file name to the constructor. Its arguments are the name of the file and the file access mode flags. You can use several flags at a time simply by <font color="#008000">OR</font>ing their values together, as you can see in the previous 
listing. These flags, which describe how to open the file and which specify the types of valid operations, are defined as part of the <font color="#008000">CFile</font> class. They are listed in Table 8.2 along with their descriptions.</P>

<P><I>Table 8.2&#151;The File Mode Flags</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Flag</B></P>

<TD>

<P><B>Description</B></P>

<TR>

<TD>

<pre><font color="#008000">CFile::modeCreate</font></pre>

<TD>

<P>Creates a new file or truncates an existing file to length 0</P>

<TR>

<TD>

<pre><font color="#008000">CFile::modeNoInherit</font></pre>

<TD>

<P>Disallows inheritance by a child process</P>

<TR>

<TD>

<pre><font color="#008000">CFile::modeNoTruncate</font></pre>

<TD>

<P>When creating the file, does not truncate the file if it already exists</P>

<TR>

<TD>

<pre><font color="#008000">CFile::modeRead</font></pre>

<TD>

<P>Allows read operations only</P>

<TR>

<TD>

<pre><font color="#008000">CFile::modeReadWrite</font></pre>

<TD>

<P>Allows both read and write operations</P>

<TR>

<TD>

<pre><font color="#008000">CFile::modeWrite</font></pre>

<TD>

<P>Allows write operations only</P>

<TR>

<TD>

<pre><font color="#008000">CFile::shareCompat</font></pre>

<TD>

<P>Allows other processes to open the file</P>

<TR>

<TD>

<pre><font color="#008000">CFile::shareDenyNone</font></pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -