提供以下内容,恕我直言,接受的答案不起作用。那一个只打印目录并且不能正确递归。
#include <sys/types.h>
#include <sys/param.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
static int listDir_helper(char* path) {
  char slash = '\\';  // or maybe slash = '/'
  DIR* dir;
  struct dirent *ent;
  char *NulPosition = &path[strlen(path)];
  if ((dir = opendir(path)) != NULL) {
    while ((ent = readdir(dir)) != NULL) {
      printf("%s%c%s\n", path, slash, ent->d_name);
      if (ent->d_type == DT_DIR) {
        if ((strcmp(ent->d_name, ".") != 0) && (strcmp(ent->d_name, "..") != 0)) {
          sprintf(NulPosition, "%c%s", slash, ent->d_name);
          if (listDir_helper(path)) {
            closedir(dir);
            return 1;
          }
          *NulPosition = '\0';
        }
      }
    }
  }
  closedir(dir);
  return 0;
}
int listDir(const char* path){
  struct dirent *ent;
  char pathmax[MAXPATHLEN+1+sizeof(ent->d_name)+1];
  if (strlen(path) > MAXPATHLEN) {
    return 1;
  }
  strcpy(pathmax, path);
  return listDir_helper(pathmax);
}
int main() {
  listDir2("C:\\tmp");
  return 0;
}