📄 subject_31147.htm
字号:
<p>
序号:31147 发表者:itsme 发表日期:2003-03-01 10:13:46
<br>主题:求助:关于文件读写的问题
<br>内容:问题1: <BR> CFile f; <BR> CFileException e; <BR> char buffer[5]; <BR> UINT i; <BR> char* pFileName = "test.ini"; <BR> //f.SetFilePath="C:\windows\desktop"; <BR> if( !f.Open(pFileName, CFile::modeCreate | CFile::modeWrite, &e ) ) <BR> { <BR> #ifdef _DEBUG <BR> afxDump << "File could not be opened " << e.m_cause << "\n"; <BR> #endif <BR> } <BR> //f.SeekToEnd(); <BR> i = f.Read(buffer,5); <BR> m_data=buffer; <BR> UpdateData(false); <BR> f.Close; <BR>运行代码后提示错误对话框:对一未命名文件的操作无效 <BR>在f.open里面不是把pFileName赋值给它了吗? <BR> <BR>问题2: <BR> CFile f; <BR> CFileException e; <BR> CString buffer; <BR> char* pFileName = "test.ini"; <BR> //f.SetFilePath="C:\windows\desktop"; <BR> if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, &e ) ) <BR> { <BR> #ifdef _DEBUG <BR> afxDump << "File could not be opened " << e.m_cause << "\n"; <BR> #endif <BR> } <BR> buffer="23.5"; <BR> f.Write(buffer,buffer.GetLength()); <BR> 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 + -