首页 > 代码库 > 常用的字符串文件文件夹操作
常用的字符串文件文件夹操作
自己整理的一个类,需要的添加到工程中直接用就可以了。
1 /* ******* StrDirFile.h ********** 2 ********* 文件操作函数声明 ********** */ 3 4 /* author: autumoon */ 5 6 #ifndef _STR_DIR_FILE_ 7 #define _STR_DIR_FILE_ 8 9 #pragma comment(linker,"/manifestdependency:\"type=‘win32‘ name=‘Microsoft.Windows.Common-Controls‘ version=‘6.0.0.0‘ processorArchitecture=‘x86‘ publicKeyToken=‘6595b64144ccf1df‘ language=‘*‘\"")10 11 #include <afxdlgs.h> //打开文件12 #include <ShlObj.h> //浏览文件夹13 14 class CStrDirFile15 {16 public:17 CString m_strInput;18 CStrDirFile();19 CStrDirFile(const char szPath[]);20 CStrDirFile(CString strPath);21 22 public:23 bool IsDir();24 bool IsDir(CString strPath);25 bool IsFile();26 bool IsFile(CString strPath);27 28 //字符串操作29 char* CstringToChar(CString strCstring); //注意防止通过修改char*指向的内容损坏CString结构30 31 CString GetDirOfDir();//获取目录的上一级目录32 CString GetDirOfDir(CString strDirPath);//获取目录的上一级目录33 CString GetDirOfFile(); //获取文件的所在的目录34 CString GetDirOfFile(CString strFilePath); //获取文件的所在的目录35 CString GetNameOfDir(CString strDirPath); //获取某个路径的最里层的目录名36 CString GetNameOfFile(CString strFilePath, bool bWithSuffix = true); //获取全文件全路径中的文件名37 CString FloatToCstring(const float fNum, const int nDigit = 6);38 CString IntToCstring(const int nNum);39 CString ParseLineInCsv(CString strLine, const int nColumn); //返回csv行的某一列的值40 CString ShowTheInput(); //显示当前输入参数41 42 //文件操作43 bool IfExistFile();44 bool IfExistFile(const char szPath[]);45 bool IfExistFile(CString strFilePath);46 47 //后缀相关的字符串注意都使用小写48 CString OpenSuffixFile(const char szPath[]); //打开特定类型的文件,返回路径49 CString OpenSuffixFile(CString strSuffix = CString("txt")); //打开特定类型的文件,返回路径50 CString OpenSuffixFile(const int nSuffix, ...); //打开多种类型的文件,返回路径51 CString OpenFile(); //打开任意类型的文件,返回路径52 CString OpenTXT(); //打开txt文件,返回路径53 54 int ParseTXTFile(CStringArray* pArrContentInFile);55 int ParseTXTFile(const char szPath[], CStringArray* pArrContentInFile);56 int ParseTXTFile(CString strFilePath, CStringArray* pArrContentInFile);57 int SaveTXTFile(const char szPath[], CStringArray& arrContent, bool bAppend = false);58 int SaveTXTFile(CString strTxtPath, CStringArray& arrContent, bool bAppend = false);59 60 //文件夹操作61 bool IfExistDir();62 bool IfExistDir(const char szPath[]);63 bool IfExistDir(CString strDirPath);64 65 CString BrowseDir(char szTips[]);66 CString BrowseDir(CString strTips = CString("请选择文件夹")); //浏览一个文件夹67 CString BrowseDirPlus(CString strTips = CString("请选择文件夹")); //浏览一个文件夹, 带新建按钮68 69 int ReadCurrentDirs(CStringArray* pArrDirsInFolder); //读取当前目录下的目录,不包含子目录70 int ReadCurrentDirFiles(CStringArray* pArrFilesInFolder, char szSuffix[] = ".txt"); //读取当前目录下的文件,不包含子目录71 int ReadDirs(CString strPath, CStringArray* pArrDirsInFolder, bool bIncludeSub = true); //读取当前目录下的目录72 int ReadDirFiles(CString strPath, CStringArray* pArrFilesInFolder, char szSuffix[] = ".txt", bool bIncludeSub = true); //读取当前目录下的文件73 74 };75 76 77 #endif //_STR_DIR_FILE_
1 /* ******* StrFileDir.cpp ********** 2 ********* 文件操作函数实现 ********** */ 3 4 /* author: autumoon */ 5 #include "StrDirFile.h" 6 7 /* 构造函数 */ 8 CStrDirFile::CStrDirFile():m_strInput("D:\\autumoon") 9 { 10 11 } 12 13 CStrDirFile::CStrDirFile(const char szPath[]) 14 { 15 CString strPath(szPath); 16 m_strInput = strPath; 17 } 18 19 CStrDirFile::CStrDirFile(CString strPath):m_strInput(strPath) 20 { 21 22 } 23 24 bool CStrDirFile::IsDir() 25 { 26 return IfExistDir(m_strInput); 27 } 28 29 bool CStrDirFile::IsDir(CString strPath) 30 { 31 return IfExistDir(strPath); 32 } 33 34 bool CStrDirFile::IsFile() 35 { 36 return IfExistFile(m_strInput); 37 } 38 39 bool CStrDirFile::IsFile(CString strPath) 40 { 41 return IfExistFile(strPath); 42 } 43 44 CString CStrDirFile::GetDirOfDir(CString strDirPath) 45 { 46 if (strDirPath.Right(1) == ‘\\‘) 47 { 48 strDirPath = strDirPath.Mid(0, strDirPath.GetLength() - 1); 49 } 50 51 int index = strDirPath.ReverseFind(‘\\‘); 52 53 if (index != -1) 54 { 55 return strDirPath.Mid(0, index); 56 } 57 else 58 { 59 return strDirPath; 60 } 61 } 62 63 CString CStrDirFile::GetDirOfFile() 64 { 65 return GetDirOfFile(m_strInput); 66 } 67 68 CString CStrDirFile::GetDirOfFile(CString strFilePath) 69 { 70 if (IsDir(strFilePath)) 71 { 72 return strFilePath; 73 } 74 75 int index = strFilePath.ReverseFind(‘\\‘); 76 return strFilePath.Mid(index + 1, strFilePath.GetLength() - index -1); 77 } 78 79 CString CStrDirFile::OpenFile() 80 { 81 CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("所有文件(*.*)|*.*||")); 82 83 CString szFileName(""); 84 85 if (IsDir()) 86 { 87 dlg.m_ofn.lpstrInitialDir = m_strInput; 88 } 89 else 90 { 91 dlg.m_ofn.lpstrInitialDir = GetDirOfFile(); 92 } 93 94 if (dlg.DoModal() == IDOK) 95 { 96 szFileName = dlg.GetPathName(); 97 } 98 99 return szFileName;100 }101 102 CString CStrDirFile::OpenTXT()103 {104 CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("txt文件(*.txt)|*.txt|所有文件(*.*)|*.*||"));105 106 CString szFileName("");107 108 if (IsDir())109 {110 dlg.m_ofn.lpstrInitialDir = m_strInput;111 }112 else113 {114 dlg.m_ofn.lpstrInitialDir = GetDirOfFile();115 }116 117 if (dlg.DoModal() == IDOK)118 {119 szFileName = dlg.GetFileName();120 }121 122 return szFileName;123 }124 125 CString CStrDirFile::OpenSuffixFile(CString strSuffix)126 {127 if (strSuffix.Left(1) == ‘.‘)128 {129 //delete the ‘.‘ before suffix130 strSuffix = strSuffix.Mid(1, strSuffix.GetLength() - 1);131 }132 133 CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strSuffix + "文件(*." + strSuffix + ")|*." + strSuffix + "|所有文件(*.*)|*.*||");134 135 CString szFileName("");136 137 if (IsDir())138 {139 dlg.m_ofn.lpstrInitialDir = m_strInput;140 }141 else142 {143 dlg.m_ofn.lpstrInitialDir = GetDirOfFile();144 }145 146 if (dlg.DoModal() == IDOK)147 {148 szFileName = dlg.GetPathName();149 }150 151 return szFileName;152 }153 154 CString CStrDirFile::OpenSuffixFile(const int nSuffix, ...)155 {156 va_list argp;157 char* para;158 va_start(argp, nSuffix);159 160 CStringArray arrSuffixs;161 CString strSuffix;162 for (int i = 0; i < nSuffix; i++)163 {164 strSuffix = va_arg(argp, char*);165 arrSuffixs.Add(strSuffix);166 }167 va_end(argp);168 169 //打开多种类型170 for (int i = 0; i < nSuffix; i++)171 {172 if (arrSuffixs[i].Left(1) == ‘.‘)173 {174 //delete the ‘.‘ before suffix175 arrSuffixs[i] = arrSuffixs[i].Mid(1, arrSuffixs[i].GetLength() - 1);176 }177 }178 179 CString strTemp("");180 for (int i = 0; i < nSuffix; i++)181 {182 strTemp += arrSuffixs[i] + "文件(*." + arrSuffixs[i] + ")|*." + arrSuffixs[i] + "|";183 }184 185 CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strTemp + "所有文件(*.*)|*.*||");186 187 CString szFileName("");188 189 if (IsDir())190 {191 dlg.m_ofn.lpstrInitialDir = m_strInput;192 }193 else194 {195 dlg.m_ofn.lpstrInitialDir = GetDirOfFile();196 }197 198 if (dlg.DoModal() == IDOK)199 {200 szFileName = dlg.GetPathName();201 }202 203 return szFileName;204 }205 206 bool CStrDirFile::IfExistFile()207 {208 return IfExistFile(m_strInput);209 }210 211 bool CStrDirFile::IfExistFile(const char szPath[])212 {213 CString strFilePath = CString(szPath);214 return IfExistFile(strFilePath);215 }216 217 bool CStrDirFile::IfExistFile(CString strFilePath)218 {219 CFile file;220 if (file.Open(strFilePath,CFile::modeRead))221 {222 file.Close();223 return true;224 }225 return false;226 }227 228 CString CStrDirFile::OpenSuffixFile(const char szPath[])229 {230 CString strPath(szPath);231 return OpenSuffixFile(strPath);232 }233 234 int CStrDirFile::ParseTXTFile(CStringArray* pArrContentInFile)235 {236 return ParseTXTFile(m_strInput, pArrContentInFile);237 }238 239 int CStrDirFile::ParseTXTFile(const char szPath[], CStringArray* pArrContentInFile)240 {241 CString strFilePath(szPath);242 return ParseTXTFile(strFilePath, pArrContentInFile);243 }244 245 int CStrDirFile::ParseTXTFile(CString strFilePath, CStringArray* pArrContentInFile)246 {247 CStdioFile file;248 file.Open(strFilePath, CFile::modeRead);249 250 if (!file.m_pStream)251 {252 return -1;253 }254 CString szLine;255 while(file.ReadString(szLine))256 {257 pArrContentInFile->Add(szLine);258 }259 260 return 0;261 }262 263 int CStrDirFile::SaveTXTFile(const char szPath[], CStringArray& arrContent, bool bAppend)264 {265 CString strTxtPath(szPath);266 SaveTXTFile(strTxtPath, arrContent, bAppend);267 268 return 0;269 }270 271 int CStrDirFile::SaveTXTFile(CString strTxtPath, CStringArray& arrContent, bool bAppend)272 {273 CStdioFile file;274 if (bAppend)275 {276 file.Open(strTxtPath, CFile::modeCreate|CFile::modeReadWrite|CFile::modeNoTruncate);277 file.SeekToEnd();278 }279 else280 {281 file.Open(strTxtPath, CFile::modeCreate|CFile::modeReadWrite);282 }283 284 for (int i = 0; i < arrContent.GetCount(); i++)285 {286 file.WriteString(arrContent[i]);287 }288 file.Close();289 290 return 0;291 }292 293 294 bool CStrDirFile::IfExistDir()295 {296 return IfExistDir(m_strInput);297 }298 299 bool CStrDirFile::IfExistDir(const char szPath[])300 {301 CString strDirPath(szPath);302 303 return IfExistDir(strDirPath);304 }305 306 bool CStrDirFile::IfExistDir(CString strDirPath)307 {308 //本方法不能判断根目录309 WIN32_FIND_DATA fd;310 bool ret = FALSE;311 HANDLE hFind = FindFirstFile(strDirPath, &fd);312 if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))313 {314 //目录存在315 ret = TRUE;316 317 }318 FindClose(hFind);319 320 return ret;321 }322 323 CString CStrDirFile::BrowseDir(char szTips[]/* = "请选择文件夹"*/)324 {325 CString strTips = CString(szTips);326 return BrowseDir(strTips);327 }328 329 CString CStrDirFile::BrowseDir(CString strTips/* = CString("请选择文件夹")*/)330 {331 CString szFileFolderPath;332 TCHAR pszPath[MAX_PATH];333 BROWSEINFO biFolder;334 biFolder.hwndOwner = NULL;335 biFolder.pidlRoot = NULL;336 biFolder.pszDisplayName = NULL;337 biFolder.lpszTitle = strTips;338 biFolder.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;339 biFolder.lpfn = NULL;340 biFolder.lParam = 0;341 342 LPITEMIDLIST pidl = SHBrowseForFolder(&biFolder);343 if (!pidl)344 {345 return "";346 }347 else348 {349 SHGetPathFromIDList(pidl, pszPath);350 m_strInput = pszPath;351 return pszPath;352 }353 }354 355 CString CStrDirFile::BrowseDirPlus(CString strTips/* = CString("请选择文件夹")*/)356 {357 CString szFileFolderPath;358 TCHAR pszPath[MAX_PATH];359 BROWSEINFO biFolder;360 biFolder.hwndOwner = NULL;361 biFolder.pidlRoot = NULL;362 biFolder.pszDisplayName = NULL;363 biFolder.lpszTitle = strTips;364 biFolder.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_NEWDIALOGSTYLE;365 biFolder.lpfn = NULL;366 biFolder.lParam = 0;367 368 LPITEMIDLIST pidl = SHBrowseForFolder(&biFolder);369 if (!pidl)370 {371 return "";372 }373 else374 {375 SHGetPathFromIDList(pidl, pszPath);376 m_strInput = pszPath;377 return pszPath;378 }379 }380 381 CString CStrDirFile::GetNameOfDir(CString strDirPath)382 {383 int index = strDirPath.ReverseFind(‘\\‘);384 return strDirPath.Mid(index + 1, strDirPath.GetLength() - index -1);385 }386 387 CString CStrDirFile::GetNameOfFile(CString strFilePath, bool bWithSuffix)388 {389 int index = strFilePath.ReverseFind(‘\\‘);390 CString strFileName = strFilePath.Mid(index + 1, strFilePath.GetLength() - index - 1);391 392 if (bWithSuffix)393 {394 return strFileName;395 }396 else397 {398 int nIndexOfDot = strFileName.ReverseFind(‘.‘);399 if (nIndexOfDot == -1)400 {401 return strFileName;402 }403 else404 {405 return strFileName.Mid(0, nIndexOfDot);406 }407 }408 409 }410 411 CString CStrDirFile::FloatToCstring(const float fNum, const int nDigit)412 {413 CString strTemp;414 strTemp.Format(_T("%.") + IntToCstring(nDigit) + _T("lf"), fNum);415 return strTemp;416 }417 418 CString CStrDirFile::IntToCstring(const int nNum)419 {420 CString strTemp;421 strTemp.Format(_T("%d"), nNum);422 return strTemp;423 }424 425 CString CStrDirFile::ParseLineInCsv(CString strLine, const int nColumn)426 {427 CString strContent;428 AfxExtractSubString(strContent, strLine, nColumn, ‘,‘);429 return strContent;430 }431 432 CString CStrDirFile::ShowTheInput()433 {434 return m_strInput;435 }436 437 char* CStrDirFile::CstringToChar(CString strCstring)438 {439 #ifdef _UNICODE440 USES_CONVERSION;441 return W2A(strCstring);442 #else443 return (LPSTR)(LPCTSTR)strCstring;444 #endif445 }446 447 int CStrDirFile::ReadCurrentDirs(CStringArray* pArrDirsInFolder)448 {449 if (IsFile())450 {451 return -1;452 }453 454 return ReadDirs(m_strInput, pArrDirsInFolder, false);455 }456 457 int CStrDirFile::ReadCurrentDirFiles(CStringArray* pArrFilesInFolder, char szSuffix[]/* = ".txt"*/)458 {459 if (IsFile())460 {461 return -1;462 }463 464 return ReadDirFiles(m_strInput, pArrFilesInFolder, szSuffix, false);465 }466 467 int CStrDirFile::ReadDirs(CString strPath, CStringArray* pArrDirsInFolder, bool bIncludeSub/* = true*/)468 {469 CFileFind ff; 470 DWORD size = 0; 471 CString szDir = strPath + _T("\\*.*"); //搜索路径,包括所有子目录 472 BOOL ret = ff.FindFile(szDir); 473 474 while (ret) 475 { 476 ret = ff.FindNextFile(); 477 478 if(!ff.IsDots()) 479 { 480 if(ff.IsDirectory() && !ff.IsHidden()) 481 { 482 //子目录结点,递归483 pArrDirsInFolder->Add(ff.GetFilePath());484 if (bIncludeSub)485 {486 ReadDirs(ff.GetFilePath(), pArrDirsInFolder);487 }488 } 489 } 490 }491 492 return 0;493 }494 495 int CStrDirFile::ReadDirFiles(CString strPath, CStringArray* pArrFilesInFolder, char szSuffix[]/* = "txt"*/, bool bIncludeSub/* = true*/)496 {497 CFileFind ff; 498 DWORD size = 0; 499 500 CString szDir = strPath + _T("\\*.*"); //搜索路径,包括所有子目录 501 BOOL ret = ff.FindFile(szDir); 502 503 if (IsFile(strPath))504 {505 return -1;506 }507 508 while (ret) 509 { 510 ret = ff.FindNextFile(); 511 512 if(!ff.IsDots()) 513 { 514 if(ff.IsDirectory() && bIncludeSub) 515 { 516 //子目录结点,递归517 ReadDirFiles(ff.GetFilePath(), pArrFilesInFolder, szSuffix, bIncludeSub);518 } 519 else 520 { 521 if (ff.GetFileName().MakeLower().Find(CString(szSuffix)) != -1)522 {523 pArrFilesInFolder->Add(ff.GetFilePath());524 }525 } 526 } 527 }528 529 return 0;530 }
OK! Enjoy it!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。