subject_35537.htm

来自「vc」· HTM 代码 · 共 30 行

HTM
30
字号
<p>
序号:35537 发表者:lijianxu 发表日期:2003-04-07 18:36:48
<br>主题:文件操作的问题?
<br>内容:如何用CFile来打开或保存文件?具体点。谢谢
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:西北狼 回复日期:2003-04-07 18:49:48
<br>内容:CFile yourfile;<BR>yourfile.Open("路径加文件名",[打开方式]CFile::modeCreate|CFile::modeRead(写)|<BR>CFile::modeWrite(读));<BR>yourfile.Write(str,sizeof(str));//写<BR>yourfile.Read(str,sizeof(str));//读<BR><BR>读指定位置的数据:<BR>yourfile.Seek(偏移量,CFile::begin(CFile::end or CFile::current));<BR>文件指针偏移:在文件开始位置偏移(偏移量)个单位;<BR>文件指针偏移:在文件结束位置偏移(偏移量)个单位;<BR>文件指针偏移:在文件当前位置偏移(偏移量)个单位;<BR><BR>e.g:<BR>&nbsp;&nbsp; int j=2000;<BR>&nbsp;&nbsp; long int k;&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp; CString newstr=_T("shang");<BR>&nbsp;&nbsp; CFile file;<BR>&nbsp;&nbsp; char str[5];<BR>&nbsp;&nbsp; file.Open("shang.txt",CFile::modeCreate|CFile::modeReadWrite);<BR>&nbsp;&nbsp; file.Write(newstr,newstr.GetLength());<BR>&nbsp;&nbsp; file.Write(&amp;j,sizeof(j));<BR>&nbsp;&nbsp;<BR>&nbsp;&nbsp; file.Read(&amp;k,sizeof(k));<BR>&nbsp;&nbsp;// file.SeekToBegin();<BR>&nbsp;&nbsp; file.Seek(0,CFile::begin); <BR>&nbsp;&nbsp; int t=newstr.GetLength();<BR>&nbsp;&nbsp; file.Read(str,newstr.GetLength()-1 ) ; <BR>&nbsp;&nbsp; file.Close();<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:lijianxu 回复日期:2003-04-09 13:31:51
<br>内容:yourfile.Open("路径加文件名",[打开方式]CFile::modeCreate|CFile::modeRead(写)|<BR>&nbsp;&nbsp;CFile::modeWrite(读));<BR>再打入CFile和::后列出来的属性和方法中没有modeCreate这类东东。请解释<BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:zhaiyl 回复日期:2003-04-09 14:30:06
<br>内容:CFile::modeCreate <BR> Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length.<BR><BR><BR>CFile::modeNoTruncate&nbsp;&nbsp;<BR> Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.<BR><BR><BR>CFile::modeRead&nbsp;&nbsp; <BR>Opens the file for reading only.<BR><BR><BR>CFile::modeReadWrite&nbsp;&nbsp; Opens the file for reading and writing.<BR><BR><BR>CFile::modeWrite&nbsp;&nbsp; Opens the file for writing only.<BR><BR><BR>CFile::modeNoInherit&nbsp;&nbsp; Prevents the file from being inherited by child processes.<BR><BR><BR>CFile::shareDenyNone&nbsp;&nbsp; Opens the file without denying other processes read or write access to the file. Create fails if the file has been opened in compatibility mode by any other process.<BR><BR><BR>CFile::shareDenyRead&nbsp;&nbsp; Opens the file and denies other processes read access to the file. Create fails if the file has been opened in compatibility mode or for read access by any other process.<BR><BR><BR>CFile::shareDenyWrite&nbsp;&nbsp; Opens the file and denies other processes write access to the file. Create fails if the file has been opened in compatibility mode or for write access by any other process.<BR><BR><BR>CFile::shareExclusive&nbsp;&nbsp; Opens the file with exclusive mode, denying other processes both read and write access to the file. Construction fails if the file has been opened in any other mode for read or write access, even by the current process.<BR><BR><BR>CFile::shareCompat&nbsp;&nbsp; This flag is not available in 32 bit MFC. This flag maps to CFile::shareExclusive when used in CFile::Open.<BR><BR><BR>CFile::typeText&nbsp;&nbsp; Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).<BR><BR><BR>CFile::typeBinary&nbsp;&nbsp; Sets binary mode (used in derived classes only). <BR><BR>很好的呦,来自MSDN,多查查吧
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:lijianxu 回复日期:2003-04-09 14:48:52
<br>内容:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CString&nbsp;&nbsp;FilePathName;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char buffer[1024];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//char szFilter[]="All files (*.*)|*.*|Text files(*.txt)|*.txt";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFileDialog file(TRUE);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(file.DoModal()==IDOK)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FilePathName=file.GetFileName();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CFile cfile;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cfile.Open(FilePathName,CFile::modeRead | CFile::modeWrite,NULL);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cfile.Read(buffer,1024);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_receive_edit.SetWindowText(buffer);<BR>这段程序错在哪里啊?m_receive_edit是一个显示打开文件的字符。再打入CFile;;后不会出现modeRead 或 modeWrite
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

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