tft每日頭條

 > 科技

 > mfc多線程同時訪問同一數據

mfc多線程同時訪問同一數據

科技 更新时间:2025-03-20 09:52:44

mfc多線程同時訪問同一數據?在這片文章中将講訴用C 實現實現遍曆指定文件和文件夾,一個是基于Win32平台開發的,一個是基于MFC平台開發的,在這裡貼出來與大家分享在完成過程中參考了這篇博客,我來為大家科普一下關于mfc多線程同時訪問同一數據?下面希望有你要的答案,我們一起來看看吧!

mfc多線程同時訪問同一數據(CMFC兩種方法遍曆指定文件或文件夾)1

mfc多線程同時訪問同一數據

1. 概述

在這片文章中将講訴用C 實現實現遍曆指定文件和文件夾,一個是基于Win32平台開發的,一個是基于MFC平台開發的,在這裡貼出來與大家分享。在完成過程中參考了這篇博客

2. 基于Win32平台開發

定義的兩個成員變量

private: std::vector<std::string> filepath; //保存圖像數據的位置信息 char m_szInitDir[1000]; //初始文件路徑

由給定的文件夾進行遍曆

//************************************************************************ // 函數名稱: ReadFilenameFromFolder // 訪問權限: public // 創建日期: 2016/12/26 // 創 建 人: // 函數說明: 讀取一個目錄下所有的Dcm文件名 // 函數參數: std::string foldername 文件夾所在的文件夾路徑 // 返 回 值: bool //************************************************************************ bool CDcmRead::ReadFilenameFromFolder(std::string foldername) { if (!this->GetFolderName(foldername)) //判斷路徑可用性 { std::cout << "輸入文件路徑錯誤無法獲取" << std::endl; return false; } foldername = this->m_szInitDir; if (!this->BrowseFolder(foldername.c_str())) { std::cout << "遍曆文件夾失敗" << std::endl; return false; } return true; } //************************************************************************ // 函數名稱: BrowseFolder // 訪問權限: public // 創建日期: 2016/12/26 // 創 建 人: // 函數說明: 遍曆文件夾,提取出目錄下和子目錄的所有dcm文件名 // 函數參數: const char * foler_name 文件夾名稱 // 返 回 值: bool //************************************************************************ bool CDcmRead::BrowseFolder(const char* foler_name) { _chdir(foler_name); //首先查找dir中符合要求的文件 long hFile; _finddata_t fileinfo; if ((hFile = _findfirst("*.dcm", &fileinfo)) != -1) //這裡的 *.dcm 是我感興趣的文件後綴名 { do { //檢查是不是目錄 //如果不是,則進行處理 if (!(fileinfo.attrib & _A_SUBDIR)) { char filename[_MAX_PATH]; strcpy(filename, foler_name); strcat(filename, fileinfo.name); #ifdef DEBUG std::cout << filename << std::endl; #endif // DEBUG this->filepath.push_back(filename); } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } //查找dir中的子目錄 //因為在處理dir中的文件時,派生類的ProcessFile有可能改變了 //當前目錄,因此還要重新設置當前目錄為dir。 //執行過_findfirst後,可能系統記錄下了相關信息,因此改變目錄 //對_findnext沒有影響。 _chdir(foler_name); if ((hFile = _findfirst("*.*", &fileinfo)) != -1) { do { //檢查是不是目錄,如果是,再檢查是不是 . 或 .. //如果不是,進行叠代 if ((fileinfo.attrib & _A_SUBDIR)) { if (strcmp(fileinfo.name, ".") != 0 && strcmp (fileinfo.name, "..") != 0) { char subdir[_MAX_PATH]; strcpy(subdir, foler_name); strcat(subdir, fileinfo.name); strcat(subdir, "\\"); #ifdef DEBUG std::cout << "找到子文件夾: " << subdir << std::endl; #endif // DEBUG if (!this->BrowseFolder(subdir)) return false; } } } while (_findnext(hFile, &fileinfo) == 0); _findclose(hFile); } return true; } //************************************************************************ // 函數名稱: GetFolderName // 訪問權限: public // 創建日期: 2016/12/26 // 創 建 人: // 函數說明: 規整化目錄 // 函數參數: std::string foldername 傳入的文件夾路徑 // 返 回 值: bool //************************************************************************ bool CDcmRead::GetFolderName(std::string foldername) { //先把dir轉換為絕對路徑 if (_fullpath(m_szInitDir, foldername.c_str(), _MAX_PATH) == NULL) { std::cout << "GetFolderName:獲取輸入路徑的絕對路徑失敗!" << std::endl; return false; } //判斷目錄是否存在 if (_chdir(m_szInitDir) != 0) { std::cout << "GetFolderName:輸入的路徑不存在!" << std::endl; return false; } //如果目錄的最後一個字母不是'\',則在最後加上一個'\' int len = strlen(m_szInitDir); if (m_szInitDir[len - 1] != '\\') strcat(m_szInitDir, "\\"); return true; }

程序遞歸調用,将所有指定的文件後綴文件保存在filepath這個類成員變量中。

3. 基于MFC平台開發

//************************************************************************ // 函數名稱: InitFileTree // 訪問權限: public // 創建日期: 2016/10/31 // 創 建 人: // 函數說明: 遍曆目錄下的所有文件夾和文件 // 函數參數: CString path 需要遍曆的文件夾 // 函數參數: HTREEITEM Parent 節點的父結點 // 返 回 值: BOOL //************************************************************************ BOOL CMyMenueTab1::InitFileTree(CString path, HTREEITEM Parent) { if (path == _T("")) { return FALSE; } else { if (path.Right(1) != _T("\\")) path = L"\\"; path = path _T("*.*"); } CFileFind finder; CString strPath; BOOL bWorking = finder.FindFile(path); while (bWorking) { bWorking = finder.FindNextFile(); strPath = finder.GetFilePath(); if (finder.IsDirectory() && !finder.IsDots()) { CString m_FolderName = strPath.Right(strPath.GetLength() - path.GetLength() 3); HTREEITEM m_subParent = this->m_FileTree.InsertItem(m_FolderName, 0, 0, Parent, TVI_LAST); this->InitFileTree(strPath, m_subParent); //遞歸調用 } //文件夾 else if (!finder.IsDirectory() && !finder.IsDots()) { CString m_FolderName = strPath.Right(strPath.GetLength() - path.GetLength() 3); this->m_FileTree.InsertItem(m_FolderName, 1, 1, Parent, TVI_LAST); } //文件 } return TRUE; }

面的這段代碼相對于Win32的要簡潔了很多,傳入一個路徑之後就開始讀取指定文件夾下的所有文件和文件夾。運行的到的結果:

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关科技资讯推荐

热门科技资讯推荐

网友关注

Copyright 2023-2025 - www.tftnews.com All Rights Reserved