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

📄 subject_26894.htm

📁 一些关于vc的问答
💻 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>&nbsp;&nbsp; LPCTSTR lpszFileName,<BR>&nbsp;&nbsp; UINT nOpenFlags,<BR>&nbsp;&nbsp; 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>&nbsp;&nbsp; LPCTSTR lpszFileName,<BR>&nbsp;&nbsp; UINT nOpenFlags,<BR>&nbsp;&nbsp; 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nFileLength=file.GetLength();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *lpBuffer=new char[nBufferSize+1];//要多定义一个字符,以便给它赋值为字符串结束字符<BR>&nbsp;&nbsp;&nbsp;&nbsp;ASSERT(lpBuffer!=NULL);//确保内存分配成功<BR>&nbsp;&nbsp;&nbsp;&nbsp;while(nFileLength&gt;0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;int nSize=nBufferSize;<BR>&nbsp;&nbsp;&nbsp;&nbsp;if(nSize&gt;nFileLength) nSize=nFileLength;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; file.Read(lpBuffer,nSize);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lpBuffer[nSize]='\0';<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_strLicenseText+=CString(lpBuffer);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nFileLength-=nSize;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;file.Close();<BR>delete [] lpBuffer;//注意要销毁使用New分配这一块内存<BR>lpBuffer=NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR>&nbsp;&nbsp;&nbsp;&nbsp;else <BR>&nbsp;&nbsp;&nbsp;&nbsp;{<BR>&nbsp;&nbsp;&nbsp;&nbsp;this-&gt;MessageBox("文件打开失败!","错误",MB_OK|MB_ICONSTOP);<BR>&nbsp;&nbsp;&nbsp;&nbsp;}<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;this-&gt;UpdateData(FALSE);&nbsp;&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;<BR>return TRUE;&nbsp;&nbsp;// return TRUE unless you set the focus to a control<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 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 + -