📄 subject_26894.htm
字号:
<p>
序号:26894 发表者:曲一 发表日期:2003-01-06 15:07:35
<br>主题:读文件的问题
<br>内容:想把一个1.4M的文件一次读入内存,写了下面的代码,总是出现异常错误,谁来帮我看看啊,救命啊。<BR><BR>BYTE size1[65536];<BR>memset(size1,0,sizeof(size1));<BR>CFile file;<BR>file.Open(FilePathName,CFile::begin);<BR>file.Read(size1,636);<BR>file.Close();
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
回复者:mm2003 回复日期:2003-01-06 15:11:48
<br>内容:使用NEW来开空间<BR>byte*size1=new byte[65536];<BR>...操作<BR>最后释放开辟的空间delete [] size1;
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:PLB 回复日期:2003-01-06 15:23:59
<br>内容:你的文件open函数用的不对!!!<BR>virtual BOOL Open(<BR> LPCTSTR lpszFileName,<BR> UINT nOpenFlags,<BR> CFileException* pError = NULL <BR>);<BR>Parameters<BR>lpszFileName <BR>A string that is the path to the desired file. The path can be relative, absolute, or a network name (UNC). <BR>nOpenFlags <BR>A UINT that defines the file's sharing and access mode. It specifies the action to take when opening the file. You can combine options by using the bitwise-OR ( | ) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. See the CFile constructor for a list of mode options. <BR>pError <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>
回复者:ohye212 回复日期:2003-01-06 15:24:35
<br>内容:static BYTE size1[65536];<BR>memset(size1,0,sizeof(size1));<BR>CFile file;<BR>file.Open(FilePathName,CFile::begin);<BR>file.Read(size1,65536);<BR>file.Close();
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:曲一 回复日期:2003-01-06 15:38:30
<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>
回复者:ohye212 回复日期:2003-01-06 15:55:41
<br>内容:你的文件大小固定吗?<BR>可能是不固定 file.Read(size1,65536);产生错误!!!!!!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:曲一 回复日期:2003-01-06 16:02:47
<br>内容:文件大小是1433600,因为read有限制大小,所以想一块一块的读的,<BR>这部分是从开始读出来65536个字节,这样读不行吗?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:catandmouse 回复日期:2003-01-06 16:04:49
<br>内容:你把文件读取改成一下试试<BR>CFile file;<BR>file.Open(FilePathName,CFile::modeRead);<BR>file.SeekToBegin();<BR>file.Read(size1,636);<BR>file.Close();
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:浩公 回复日期:2003-01-06 16:06:49
<br>内容:你的文件open函数用的不对!!!<BR>virtual BOOL Open(<BR> LPCTSTR lpszFileName,<BR> UINT nOpenFlags,<BR> CFileException* pError = NULL <BR>);<BR>Parameters<BR>lpszFileName <BR>A string that is the path to the desired file. The path can be relative, absolute, or a network name (UNC). <BR>nOpenFlags <BR>A UINT that defines the file's sharing and access mode. It specifies the action to take when opening the file. You can combine options by using the bitwise-OR ( | ) operator. One access permission and one share option are required; the modeCreate and modeNoInherit modes are optional. See the CFile constructor for a list of mode options. <BR>pError <BR>文件没有打开,所以出错 <BR><BR><BR><BR>引上面仁兄的话。。。<BR><BR>你的打开方式不对,你改了吗?<BR>至少应该这样:<BR><BR>CFile file;<BR>file.Open(filename,CFile::modeRead)<BR>CFile::begin是文件开始的意思, 只有seek的时候才用到。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:金枪鱼 回复日期:2003-01-06 17:43:38
<br>内容:CFile类的Open函数参数不对!!!!<BR>文件一旦打开,确实就是在文件的开头,不用移动文件位置指针。<BR><BR>看看我的一段程序:对话框从文件中读取数据,构造成一个整的CString,然后通过多行编辑控件显示。<BR><BR>BOOL CPermitDlg::OnInitDialog() <BR>{<BR>CDialog::OnInitDialog();<BR>// TODO: Add extra initialization here<BR><BR>//设置编辑控件只读<BR>m_ctrlLicenseText.SetReadOnly();<BR><BR>//通过分段读取文件中记录,每次读取的数量由nBufferSize决定<BR>CFile file;<BR>int nFileLength=0;<BR>int nBufferSize=512;<BR>CString m_strFilePath=CurWorkD;<BR>m_strFilePath+="\\license.dat";<BR>if(file.Open(m_strFilePath,CFile::modeRead))<BR>{<BR> nFileLength=file.GetLength();<BR> char *lpBuffer=new char[nBufferSize+1];//要多定义一个字符,以便给它赋值为字符串结束字符<BR> ASSERT(lpBuffer!=NULL);//确保内存分配成功<BR> while(nFileLength>0)<BR> { <BR> int nSize=nBufferSize;<BR> if(nSize>nFileLength) nSize=nFileLength;<BR> file.Read(lpBuffer,nSize);<BR> lpBuffer[nSize]='\0';<BR> m_strLicenseText+=CString(lpBuffer);<BR> nFileLength-=nSize;<BR> }<BR> file.Close();<BR>delete [] lpBuffer;//注意要销毁使用New分配这一块内存<BR>lpBuffer=NULL;<BR> }<BR> else <BR> {<BR> this->MessageBox("文件打开失败!","错误",MB_OK|MB_ICONSTOP);<BR> }<BR><BR> this->UpdateData(FALSE); <BR> <BR>return TRUE; // return TRUE unless you set the focus to a control<BR> // EXCEPTION: OCX Property Pages should return FALSE<BR>}
<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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -