读取txt文件到内存中
void CMouseWheel_TestDlg::OnBnClickedButton3()
{ // TODO: 在此添加控件通知处理程序代码 CFile myFile; CFileException ex; if (!myFile.Open("D:\\myfile.txt",CFile::modeRead,&ex)) { MessageBox("未打开文件"); TCHAR szCause[255]; CString strFormatted; ex.GetErrorMessage(szCause, 255); strFormatted = _T("The data file could not be opened because of this error: "); strFormatted += szCause; AfxMessageBox(strFormatted); return; } DWORD v_dwFileLength; v_dwFileLength=myFile.GetLength(); if (v_dwFileLength==0) { MessageBox("文件为空"); return; } myFile.Seek(0,CFile::begin); char *buffer=new char[v_dwFileLength]; CString Show; myFile.Read(buffer,v_dwFileLength); myFile.Close(); Show=buffer; delete[] buffer; MessageBox(Show); } |
|