zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    ssh免密码登录
    nginx做负载均衡+keepalived(做主备)
    centos之Too many open files问题-修改linux最大文件句柄数
    redis-JedisPoolConfig配置
    Hadoop端口说明
    hadoop 2.5 安装部署
    Java集合框架 10 连问,你有被问过吗?
    Dubbo面试八连问,这些你都能答上来吗?
    面试官:关于Java性能优化,你有什么技巧
    Docker从入门到掉坑(三):容器太多,操作好麻烦
  • 原文地址:https://www.cnblogs.com/longma8586/p/13861048.html
Copyright © 2011-2022 走看看