1.读取文件;
//跳出对话框,选择指定的文件类型
TCHAR szFilters[]= _T("DXF Files (*.dxf)|*.dxf");
// Create an Open dialog; the default file name extension is ".my".
CFileDialog fileDlg(TRUE, _T("dxf"), _T("*.dxf"),
OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters);
// Display the file dialog. When user clicks OK, fileDlg.DoModal()
// returns IDOK.
if(fileDlg.DoModal() == IDOK)
{
CString pathName = fileDlg.GetPathName();
// Implement opening and reading file in here.
//Change the window's title to the opened file's title.
CString fileName = fileDlg.GetFileTitle();
CString strLine;
CStdioFile myFile;
myFile.Open(pathName,CFile::modeRead);
//逐行读取
while(myFile.ReadString(strLine))
{
if(strLine=="AcDbLine")
{
do
{
myFile.ReadString(strLine);
strLine.Trim();
if(strLine=="10")
{
myLinePoint.push_back(LinePoint());
myFile.ReadString(strLine);
myLinePoint[i].start_x=_tstof(strLine);
}
if(strLine=="31")
{
myFile.ReadString(strLine);
myLinePoint[i].end_z=_tstof(strLine);
i++;
}
} while (strLine!="0");
}
}
2.跳出对话框选择文件目录,list勾选所选择的项目,读取文本文档,写入文本档,进度条;
void CFurtherConvertCutCodeView::OnBnClickedButtonBrowser() { CString filePath; m_ProgressFile.SetPos(0); m_ListFiles.ResetContent(); TCHAR szPath[_MAX_PATH]; BROWSEINFO bi; bi.hwndOwner=NULL; bi.pidlRoot=NULL; bi.lpszTitle=_T("文件浏览"); bi.pszDisplayName=szPath; bi.ulFlags=BIF_RETURNONLYFSDIRS; bi.lParam=NULL; bi.lpfn=NULL; LPITEMIDLIST pItemIDList=SHBrowseForFolder(&bi); if(pItemIDList) { if(SHGetPathFromIDList(pItemIDList,szPath)) filePath=szPath; CFileFind finder; CString strFilenName; BOOL bWorking=finder.FindFile(filePath+"\\*.din"); while(bWorking) { bWorking = finder.FindNextFile(); strFilenName=(LPCTSTR)finder.GetFileName(); //str为取得一个文件名 m_ListFiles.AddString(strFilenName); } } else { filePath=""; } GetDlgItem(IDC_STATIC_Browse)->SetWindowText(filePath); } void CFurtherConvertCutCodeView::OnBnClickedButtonRun() { // TODO: Add your control notification handler code here CString strFilePath; //添加光顺后程序保存的目录 CString strPath; GetDlgItem(IDC_STATIC_Browse)->GetWindowText(strFilePath); CString strFileName; //找出勾选的项目 int iSel=m_ListFiles.GetCount(); if(iSel>0) { strPath=strFilePath+_T("\\_GCG261"); CreateDirectory(strPath,NULL); } m_ProgressFile.SetPos(0); int j=0; for(int i=0;i<iSel;i++) { int ik=m_ListFiles.GetCheck(i); if(m_ListFiles.GetCheck(i)==1) { j++; } } m_ProgressFile.SetRange(0,j); m_ProgressFile.SetStep(1); for(int i=0;i<iSel;i++) { int ik=m_ListFiles.GetCheck(i); if(m_ListFiles.GetCheck(i)==1) { m_ListFiles.GetText(i,strFileName); ReadCodeFile(strFilePath+"\\"+strFileName,strPath+"\\"+strFileName); m_ProgressFile.StepIt(); } } } void CFurtherConvertCutCodeView::ReadCodeFile(CString strFileName,CString strWriteFileName) { CStringList listLines; CString strLine; CString strPartLine; CStdioFile writeFile; bool isG01=false; bool isAddG261=false; bool isBreak=false; int i; CStdioFile myFile; myFile.Open(strFileName,CFile::modeRead); CFile mFile; mFile.Open(strWriteFileName,CFile::modeCreate|CFile::modeWrite); //逐行读取 while(myFile.ReadString(strLine)) { CArchive ar(&mFile,CArchive::store); if(strLine=="M07") { i=0; while(isBreak!=true) { myFile.ReadString(strLine); i=i+1; if(strLine.Find(_T("G01"))!=-1&&strLine.Find(_T("X"))!=-1&&strLine.Find(_T("Y"))!=-1) { isG01=true; i=1; } if(isG01==true && i==2) { if(strLine.Find(_T("G02"))>-1||strLine.Find(_T("G03"))>-1) { isAddG261=true; isBreak=true; } } if(strLine.Find(_T("M08"))!=-1) { isBreak=true; } listLines.AddTail(strLine); }//end while isBreak=false; if(isAddG261==true) { strLine="G261\r\nM07"; } else { strLine="M07"; } }//end if M07 if(strLine.Find(_T("M08"))!=-1&&isAddG261==true) { strLine="G260\r\nM08"; isAddG261=false; } ar.WriteString(strLine); ar.WriteString(_T("\r\n")); POSITION rPos; rPos=listLines.GetHeadPosition(); while(rPos!=NULL) { strPartLine=listLines.GetNext(rPos); ar.WriteString(strPartLine); ar.WriteString(_T("\r\n")); } listLines.RemoveAll(); ar.Close(); }//end while mFile.Close(); }