MFC读写文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MFC读写文件相关的知识,希望对你有一定的参考价值。
(1)读文件
VOID CScale3DCControl::ReadLauFile(CString strFileName,CRichEditCtrl & richEdit) { CString strFilePath = GetApplicationPath() + strFileName; CFile file(strFilePath, CFile::modeRead); char * pBuf; DWORD dwFileLen; //定义存储文件长度的变量 dwFileLen = file.GetLength(); pBuf = new char[dwFileLen + 1]; pBuf[dwFileLen] = 0; //把最后一位一0结尾 表示文件结束 file.Read(pBuf, dwFileLen); USES_CONVERSION; richEdit.SetWindowText(A2W(pBuf)); GetKeyWordFromText(A2W(pBuf)); file.Close(); }
(2)写文件
VOID CScale3DCControl::SaveLuaFile(CRichEditCtrl & richEdit, CString strFileName) { CString strPath = GetApplicationPath() + _T("lua\") + strFileName; CFile file(strPath,CFile::modeCreate | CFile::modeWrite); CString strTemp; richEdit.GetWindowText(strTemp); USES_CONVERSION; file.Write(W2A(strTemp), strlen(W2A(strTemp))); file.Close(); }
(3)搜索文件夹下的所有文件名
void CScale3DCControl::GetFileFromDirectory(CString csDirPath, CListBox & list) //csDirPath为文件夹名字,把该文件夹底下的jpg文 //件的名字存储到容器 m_FileList中,如果不分类型 //则把csDirPath+="\*.jpg";改为csDirPat+="*." { m_FileList.clear(); list.ResetContent(); csDirPath = GetApplicationPath()+csDirPath+ _T("\*.lua"); HANDLE file; WIN32_FIND_DATA fileData; file = FindFirstFile(csDirPath.GetBuffer(), &fileData); if (file != INVALID_HANDLE_VALUE) { m_FileList.push_back(fileData.cFileName); list.AddString(fileData.cFileName); bool bState = false; bState = FindNextFile(file, &fileData); while (bState) { m_FileList.push_back(fileData.cFileName); list.AddString(fileData.cFileName); bState = FindNextFile(file, &fileData); } } else { ;// AfxMessageBox("文件夹中没有shp文件!"); } } CString CScale3DCControl::GetApplicationPath() { WCHAR buff[255] = { 0 }; ::GetModuleFileName(0, buff, 255); CString strAppFullName; strAppFullName.Format(_T("%s"), buff); CString strAppPath = _T(""); strAppPath = strAppFullName.Left(strAppFullName.ReverseFind('\') + 1); return strAppPath; }
以上是关于MFC读写文件的主要内容,如果未能解决你的问题,请参考以下文章