linux 下遍历文件夹文件

#include 
#include 
#include 
#include 

vector globVector(const string &pattern) {
    glob_t glob_result;
    glob(pattern.c_str(), GLOB_TILDE, NULL, &glob_result);
    vector files;
    for (unsigned int i = 0; i < glob_result.gl_pathc; ++i) {
        files.push_back(string(glob_result.gl_pathv[i]));
    }
    globfree(&glob_result);
    return files;
}

void mkDir(string prefix) {
  if (access(prefix.c_str(), 0) == -1)    //如果文件夹不存在
      mkdir(prefix.c_str(),S_IRUSR | S_IWUSR | S_IXUSR | S_IRWXG | S_IRWXO);
}

void getFile() {
  string path = "./data";
  DIR *dir = opendir(path.c_str());
  if (dir == NULL) {
      cout << "opendir error" << endl;
  }

  vector allPath;
  struct dirent *entry;
  while ((entry = readdir(dir)) != NULL) {

    if (entry->d_type == DT_DIR) {  //It's dir
      if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
          continue;
      string txt_path = path + entry->d_name + "*.txt";
      vector files;
      files = globVector(txt_path);
      for (size_t i = 0; i < files.size(); ++i) {
        std::cout << files[i] << std::endl;
      }

    } else {
      cout<< "is file"<< std::endl;
    }
  }
}

 

你可能感兴趣的:(linux 下遍历文件夹文件)