将文件列表保存到数组或其他内容C

时间:2012-07-02 09:40:44

标签: c arrays file-listing

我有这个功能

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;
    }
}

我想保存到数组或结构或列表或其他文件列表的结果,但我不知道如何操作。
提前谢谢!

2 个答案:

答案 0 :(得分: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]);
    }
}

请注意,我在目录上迭代两次 - 第一次获取文件计数,第二次保存结果。如果在执行此目录时添加/删除目录中的文件,则无法正常工作。

答案 1 :(得分: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);
}