1. 程式人生 > >MFC讀寫文件

MFC讀寫文件

DC stat rpath filepath use push 讀寫文件 orm reat

(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讀寫文件