0

我有这个功能

void file_listing(){
    DIR *dp;
    struct stat fileStat;
    struct dirent *ep;
    int file=0;
    dp = opendir ("./");

    if (dp != NULL){
        while ((ep = readdir(dp))){
            char *c = ep->d_name;
            char d = *c; /* first char */
            if((file=open(ep->d_name,O_RDONLY)) < -1){ 
                perror("Errore apertura file");
            }
            if(fstat(file,&fileStat)){ /*file info */
                perror("Errore funzione fstat");
            }
            if(S_ISDIR(fileStat.st_mode)){ /* directory NOT listed */
                continue;
            }
            else{
                if(d != '.'){ /* if filename DOESN'T start with . will be listed */
                    "save into someting" (ep->d_name);
                }
                else{
                    continue; /* altrimenti non lo listo */
                }
            }
        }
        (void) closedir (dp);
    }
    else{
        perror ("Impossibile aprire la directory");
        return 1;
    }
}

我想将文件列表的结果保存到数组或结构或列表或其他东西中,但我不知道该怎么做。
提前致谢!

4

2 回答 2

4

这是一个将文件列表存储在数组中并返回条目数的示例函数:

#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <malloc.h>

size_t file_list(const char *path, char ***ls) {
    size_t count = 0;
    size_t length = 0;
    DIR *dp = NULL;
    struct dirent *ep = NULL;

    dp = opendir(path);
    if(NULL == dp) {
        fprintf(stderr, "no such directory: '%s'", path);
        return 0;
    }

    *ls = NULL;
    ep = readdir(dp);
    while(NULL != ep){
        count++;
        ep = readdir(dp);
    }

    rewinddir(dp);
    *ls = calloc(count, sizeof(char *));

    count = 0;
    ep = readdir(dp);
    while(NULL != ep){
        (*ls)[count++] = strdup(ep->d_name);
        ep = readdir(dp);
    }

    closedir(dp);
    return count;
}

int main(int argc, char **argv) {

    char **files;
    size_t count;
    int i;

    count = file_list("/home/rgerganov", &files);
    for (i = 0; i < count; i++) {
        printf("%s\n", files[i]);
    }
}

请注意,我对目录进行了两次迭代——第一次是获取文件计数,第二次是保存结果。如果您在执行此操作时在目录中添加/删除文件,这将无法正常工作。

于 2012-07-02T10:27:20.480 回答
1

另一个例子,更干净但不是线程安全的,因为readdir()不是:

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

static void *realloc_or_free(void *ptr, size_t size) {
  void *tmp = realloc(ptr, size);
  if (tmp == NULL) {
    free(ptr);
  }
  return tmp;
}

static int get_dirent_dir(char const *path, struct dirent **result,
                          size_t *size) {
  DIR *dir = opendir(path);
  if (dir == NULL) {
    closedir(dir);
    return -1;
  }

  struct dirent *array = NULL;
  size_t i = 0;
  size_t used = 0;
  struct dirent *dirent;
  while ((dirent = readdir(dir)) != NULL) {
    if (used == i) {
      i += 42; // why not?
      array = realloc_or_free(array, sizeof *array * i);
      if (array == NULL) {
        closedir(dir);
        return -1;
      }
    }

    array[used++] = *dirent;
  }

  struct dirent *tmp = realloc(array, sizeof *array * used);
  if (tmp != NULL) {
    array = tmp;
  }

  *result = array;
  *size = used;

  closedir(dir);

  return 0;
}

static int cmp_dirent_aux(struct dirent const *a, struct dirent const *b) {
  return strcmp(a->d_name, b->d_name);
}

static int cmp_dirent(void const *a, void const *b) {
  return cmp_dirent_aux(a, b);
}

int main(void) {
  struct dirent *result;
  size_t size;
  if (get_dirent_dir(".", &result, &size) != 0) {
    perror("get_file_dir()");
  }

  qsort(result, size, sizeof *result, &cmp_dirent);

  for (size_t i = 0; i < size; i++) {
    printf("%s\n", result[i].d_name);
  }

  free(result);
}
于 2017-01-04T22:03:13.237 回答