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; }
c语言读写文件,如下:
VOID CScale3DCFileData::GetCharFromFile(TCHAR szChina[]) { if (!m_strFilePath)return; errno_t err; FILE *fd; err = _wfopen_s(&fd, m_strFilePath, _T("r")); INT i = 0; if (err == 0) { char buff[100] = { 0 }; while (fgets(buff, 100, fd) != NULL) { USES_CONVERSION; CString strBuff = A2W(buff); INT nLength = strBuff.GetLength(); for (INT j = 0; j < nLength - 1; j++) { if (i >= 34) { m_sFlag = strBuff[j]; break; } szChina[i++] = strBuff[j]; } } } fclose(fd); } VOID CScale3DCFileData::SaveCharToFile(TCHAR* szChina) { USES_CONVERSION; FILE * fd; errno_t err; err = _wfopen_s(&fd, m_strFilePath, _T("w") ); if (err == 0) { char * p = W2A(szChina); fputs(p, fd); } fclose(fd); }
网站栏目:MFC读写文件
本文地址:http://azwzsj.com/article/ijosgj.html