c++ 遍历目录下文件、文件夹
BOOL GetDirFiles(const char* pszDir, char* pszFileType, std::vector<std::string>& vtFileList, BOOL bRecursion)
{
	  LOGE_TRUE_RETURN_FALSE(pszDir == NULL || pszFileType == NULL)
	  char   szTem[1024] = { 0 };
	  char   szDir[1024] = { 0 };
	  strcpy(szTem, pszDir);
	  if (szTem[strlen(szTem) - 1] != '\\' || szTem[strlen(szTem) - 1] != '/')
	  {
		    strcat(szTem, "/");
	  }
	  strcpy(szDir, szTem);
	  strcat(szDir, pszFileType);
#ifdef WIN32
	  struct _finddata_t  tFileInfo = { 0 };
	  long long hFile = _findfirst(szDir, &tFileInfo);
	  if (hFile == -1)
	  {
		    return FALSE;
	  }
	  do
	  {
		    if (strcmp(tFileInfo.name, ".") == 0 || strcmp(tFileInfo.name, "..") == 0)
		    {
			      continue;
		    }
		    if ((tFileInfo.attrib   &  _A_SUBDIR) && bRecursion)
		    {
			      char   szSub[1024] = { 0 };
			      strcpy(szSub, pszDir);
			      if (szSub[strlen(szSub) - 1] != '\\' || szSub[strlen(szSub) - 1] != '/')
			      {
				        strcat(szSub, "/");
			      }
			      strcat(szSub, tFileInfo.name);
			      GetDirFiles(szSub, pszFileType, vtFileList, bRecursion);
		    }
		    else
		    {
			      vtFileList.push_back(std::string(szTem) + std::string(tFileInfo.name));
		    }
	  } while (_findnext(hFile, &tFileInfo) == 0);
	  _findclose(hFile);
#else
	  DIR* pDirInfo;
	  struct dirent* tFileInfo;
	  struct stat statbuf;
	  if ((pDirInfo = opendir(pszDir)) == NULL)
	  {
		    return FALSE;
	  }
	  while ((tFileInfo = readdir(pDirInfo)) != NULL)
	  {
		    if (strcmp(".", tFileInfo->d_name) == 0 || strcmp("..", tFileInfo->d_name) == 0)
		    {
			      continue;
		    }
		    lstat(tFileInfo->d_name, &statbuf);
		    if ((S_IFDIR & statbuf.st_mode) && bRecursion)
		    {
			      GetDirFiles(tFileInfo->d_name, pszFileType, vtFileList, bRecursion);
		    }
		    else
		    {
			      vtFileList.push_back(std::string(szTem) + std::string(tFileInfo->d_name));
		    }
	  }
	  closedir(pDirInfo);
#endif
	  return TRUE;
}
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号