VS找到工程文件的路径,并遍历此路径下的所有 .txt文件
【代码文件】:在OnBnClickedButtonLoad按钮控件下输入如下代码:
char szFileName[MAX_PATH]
m_strDataDir = szFileName; auto nDirPos = m_strDataDir.ReverseFind('\\');//ReverseFine从后向前查找"\\"符号 if (std::string::npos == nDirPos) m_strDataDir = ""; else m_strDataDir.Truncate(nDirPos + 1);
在 .h文件中添加
protected: CString m_strDataDir
在PreTranslateMessage(MSG* pMsg)下,添加如下代码,可使用上下按键切换路径下的 .txt文件
BOOL CdialogpaintDlg::PreTranslateMessage(MSG* pMsg) { if ((m_nJusyOpenedfile == 1) && (pMsg->message == WM_KEYDOWN) && ((pMsg->wParam == VK_UP) || (pMsg->wParam == VK_DOWN))) { m_nFilesCount = 0; CString strFile; CFileFind find; //===================================================================================== CString strFindStr; strFindStr = m_strDataDir; strFindStr += _T("*.txt"); BOOL bWorking = find.FindFile(strFindStr); //===================================================================================== //BOOL bWorking = find.FindFile(_T("E:/vs2019/project2019/dialog_paint/*.txt")); m_filelist.clear(); while (bWorking) { bWorking = find.FindNextFile();//开始遍历 strFile = find.GetFileName(); m_filelist.push_back(strFile); m_nFilesCount++; //OnBnClickedButtonLoad(); //遍历第一个跳到读取文件内容 }find.Close();//遍历文件夹所有txt文件,并存到strFile中 m_nJusyOpenedfile = 0; } //******************************************************************************************* CString Memory; int k = 0; int num = 0; LPCTSTR p=NULL; char buf[MAX_LINE] = { 0 }; do { if (pMsg->message == WM_KEYDOWN) { int need_refresh = 0; if (pMsg->wParam == VK_UP) { if (m_index_in_list > 0) { m_index_in_list--; need_refresh = 1; } else { Beep(400, 200); break; } } else if (pMsg->wParam == VK_DOWN) { if (m_index_in_list < m_nFilesCount - 1) { m_index_in_list++; need_refresh = 1; } else { Beep(400, 200); break; } } if (need_refresh) { char szFilename[1024] = { 0 }; CString str = m_filelist.at(m_index_in_list); SHUnicodeToAnsi(str.GetString(), szFilename, MAX_PATH); deal_with_a_full_paht_name_file(szFilename); } }//find.Close(); } while (0); return CDialogEx::PreTranslateMessage(pMsg); }
ttcbk