(1)读文件

VOIDCScale3DCControl::ReadLauFile(CStringstrFileName,CRichEditCtrl&richEdit){CStringstrFilePath=GetApplicationPath()+strFileName;CFilefile(strFilePath,CFile::modeRead);char*pBuf;DWORDdwFileLen;//定义存储文件长度的变量dwFileLen=file.GetLength();pBuf=newchar[dwFileLen+1];pBuf[dwFileLen]=0;//把最后一位一0结尾表示文件结束file.Read(pBuf,dwFileLen);USES_CONVERSION;richEdit.SetWindowText(A2W(pBuf));GetKeyWordFromText(A2W(pBuf));file.Close();}

(2)写文件

VOIDCScale3DCControl::SaveLuaFile(CRichEditCtrl&richEdit,CStringstrFileName){CStringstrPath=GetApplicationPath()+_T("lua\\")+strFileName;CFilefile(strPath,CFile::modeCreate|CFile::modeWrite);CStringstrTemp;richEdit.GetWindowText(strTemp);USES_CONVERSION;file.Write(W2A(strTemp),strlen(W2A(strTemp)));file.Close();}

(3)搜索文件夹下的所有文件名

voidCScale3DCControl::GetFileFromDirectory(CStringcsDirPath,CListBox&list)//csDirPath为文件夹名字,把该文件夹底下的jpg文//件的名字存储到容器m_FileList中,如果不分类型//则把csDirPath+="\\*.jpg";改为csDirPat+="\*."{m_FileList.clear();list.ResetContent();csDirPath=GetApplicationPath()+csDirPath+_T("\\*.lua");HANDLEfile;WIN32_FIND_DATAfileData;file=FindFirstFile(csDirPath.GetBuffer(),&fileData);if(file!=INVALID_HANDLE_VALUE){m_FileList.push_back(fileData.cFileName);list.AddString(fileData.cFileName);boolbState=false;bState=FindNextFile(file,&fileData);while(bState){m_FileList.push_back(fileData.cFileName);list.AddString(fileData.cFileName);bState=FindNextFile(file,&fileData);}}else{;//AfxMessageBox("文件夹中没有shp文件!");}}CStringCScale3DCControl::GetApplicationPath(){WCHARbuff[255]={0};::GetModuleFileName(0,buff,255);CStringstrAppFullName;strAppFullName.Format(_T("%s"),buff);CStringstrAppPath=_T("");strAppPath=strAppFullName.Left(strAppFullName.ReverseFind('\\')+1);returnstrAppPath;}

c语言读写文件,如下:

VOIDCScale3DCFileData::GetCharFromFile(TCHARszChina[]){if(!m_strFilePath)return;errno_terr;FILE*fd;err=_wfopen_s(&fd,m_strFilePath,_T("r"));INTi=0;if(err==0){charbuff[100]={0};while(fgets(buff,100,fd)!=NULL){USES_CONVERSION;CStringstrBuff=A2W(buff);INTnLength=strBuff.GetLength();for(INTj=0;j<nLength-1;j++){if(i>=34){m_sFlag=strBuff[j];break;}szChina[i++]=strBuff[j];}}}fclose(fd);}VOIDCScale3DCFileData::SaveCharToFile(TCHAR*szChina){USES_CONVERSION;FILE*fd;errno_terr;err=_wfopen_s(&fd,m_strFilePath,_T("w"));if(err==0){char*p=W2A(szChina);fputs(p,fd);}fclose(fd);}