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

📄 subject_31147.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:31147 发表者:itsme 发表日期:2003-03-01 10:13:46
<br>主题:求助:关于文件读写的问题
<br>内容:问题1: <BR>&nbsp;&nbsp; CFile f; <BR>&nbsp;&nbsp; CFileException e; <BR>&nbsp;&nbsp; char buffer[5]; <BR>&nbsp;&nbsp; UINT i; <BR>&nbsp;&nbsp; char* pFileName = "test.ini"; <BR>&nbsp;&nbsp; //f.SetFilePath="C:\windows\desktop"; <BR>&nbsp;&nbsp; if( !f.Open(pFileName, CFile::modeCreate | CFile::modeWrite, &amp;e ) ) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp; #ifdef _DEBUG <BR>&nbsp;&nbsp; afxDump &lt;&lt; "File could not be opened " &lt;&lt; e.m_cause &lt;&lt; "\n"; <BR>&nbsp;&nbsp; #endif <BR>&nbsp;&nbsp; } <BR>&nbsp;&nbsp; //f.SeekToEnd(); <BR>&nbsp;&nbsp; i = f.Read(buffer,5); <BR>&nbsp;&nbsp; m_data=buffer; <BR>&nbsp;&nbsp; UpdateData(false); <BR>&nbsp;&nbsp; f.Close; <BR>运行代码后提示错误对话框:对一未命名文件的操作无效 <BR>在f.open里面不是把pFileName赋值给它了吗? <BR>&nbsp;&nbsp;<BR>问题2: <BR>&nbsp;&nbsp; CFile f; <BR>&nbsp;&nbsp; CFileException e; <BR>&nbsp;&nbsp; CString buffer; <BR>&nbsp;&nbsp; char* pFileName = "test.ini"; <BR>&nbsp;&nbsp; //f.SetFilePath="C:\windows\desktop"; <BR>&nbsp;&nbsp; if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &amp;e ) ) <BR>&nbsp;&nbsp; { <BR>&nbsp;&nbsp; #ifdef _DEBUG <BR>&nbsp;&nbsp; afxDump &lt;&lt; "File could not be opened " &lt;&lt; e.m_cause &lt;&lt; "\n"; <BR>&nbsp;&nbsp; #endif <BR>&nbsp;&nbsp; } <BR>&nbsp;&nbsp; buffer="23.5"; <BR>&nbsp;&nbsp; f.Write(buffer,buffer.GetLength()); <BR>&nbsp;&nbsp; f.Close; <BR>这段程序把23.5这个数存到了test.ini文件中,我想要再运行该段程序,再保存一个23.5 <BR>到test.ini,可以我再次执行以后,文件里面还是只有一个23.5,为什么啊?? 
<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>回复者:upstream 回复日期:2003-03-01 10:58:44
<br>内容:问题1:因为是读文件,应该用read摸式,而你用的是打开摸式<BR>CFile::modeCreate | CFile::modeRead<BR><BR>问题2:<BR>应该用CFile::modeCreate | CFile::modeWrite | CFile :: modeNoTruncate<BR><BR>modeNoTruncate摸式,如果文件已经存在,则长度保持不变,数据在文件最后追加写入.<BR><BR><BR><BR>2003-3-1 11:00:28

⌨️ 快捷键说明

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