【问题标题】:Get (NOT display) the list of files and folder in C获取(不显示)C 中的文件和文件夹列表
【发布时间】:2015-10-05 02:49:35
【问题描述】:

我希望能够获取文件夹和文件的列表(如果可能的话带有子文件夹)并返回它(例如数组)

所以我有这个脚本:

DIR *dir;
struct dirent *ent;
if ((dir = opendir ("c:\\src\\")) != NULL) {
  /* print all the files and directories within directory */
  while ((ent = readdir (dir)) != NULL) {
    printf ("%s\n", ent->d_name);  // I dont want to print, I want to add it to a array
  }
  closedir (dir);
} else {
  /* could not open directory */
  perror ("");
  return EXIT_FAILURE;
}

所以我可以这样做:

array = getFolders('.');

现在,如果我遍历数组,我可以看到所有文件夹和文件。

eg:

array[0] = 'file1.txt';
array[1] = 'folder1/';
array[2] = 'folder1/file2.txt';
array[3] = 'folder1/file3.txt';
array[4] = 'folder2/';
array[5] = 'folder3/';
array[6] = 'folder3/filez.txt';

等等……

【问题讨论】:

  • scandir
  • @BLUEPIXY 这也会获取文件吗?
  • 为什么要将它添加到数组中?如果您只是要遍历该数组以对每个文件名执行某些操作,那么在 readdir() 循环的主体中执行此操作会更有效。
  • @JonathonReinhart 因为我将在代码中多次使用该数组。我阅读了文件夹结构,然后我使用了 4 次...我不想重新扫描 4 次结构...您会在每次需要时扫描 /home 文件夹吗?
  • @PatREllery 有一个链接目的地的示例代码。

标签: c arrays file directory subdirectory


【解决方案1】:

popenmallocrealloc 中使用DIR 命令

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

#define INC 1024

char **getFolders(const char *path, /* out */ size_t *count){
    char buff[1024];
    snprintf(buff, sizeof buff, "dir /B /S /ON \"%s\"", path);
    FILE *fp = popen(buff, "r");
    size_t capacity = INC;
    char **entry;
    struct stat finfo;

    if(NULL==(entry=malloc(capacity*sizeof(*entry)))){
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    *count = 0;
    while(fgets(buff, sizeof buff, fp)){
        size_t len;
        for(len = 0; buff[len]; ++len){
            if(buff[len] == '\\')
                buff[len] = '/';
        }
        buff[len-1] = '\0';
        stat(buff, &finfo);
        if(S_ISDIR(finfo.st_mode))
            buff[len-1] = '/';

        entry[*count] = strdup(buff);
        if(++*count == capacity){
            capacity += INC;
            if(NULL==(entry=realloc(entry, capacity*sizeof(*entry)))){
                perror("realloc");
                exit(EXIT_FAILURE);
            }
        }
    }
    pclose(fp);
    return entry;
}

int main(void){
    size_t i, count = 0;
    char **entry = getFolders("c:/src/", &count);

    for(i = 0; i < count; ++i){
        printf("%s\n", entry[i]);
        free(entry[i]);
    }
    free(entry);
    return 0;
}

【讨论】:

  • /tmp$ dir /B /S /ON . dir: cannot access /B: No such file or directory dir: cannot access /S: No such file or directory dir: cannot access /ON: No such file or directory
  • 对不起,如果您不能使用 DIR。
  • sorry if you can't use DIR. – BLUEPIXY 14 mins ago : 有时候失败也没关系吧?
【解决方案2】:

您可以通过为目录条目分配内存来分配指针列表并存储每个条目:

char **getFolder()
{

char **list = malloc(4096* sizeof *list);
size_t i = 0;
....

  while ((ent = readdir (dir)) != NULL) {
     list[i]=strdup(ent->d_name);
     i++;
     if( i >=4096 ) { /* time to realloc */}
  }

return list;
}

从调用者那里,您可以将其接收为:

char **list=getFolder();

如果条目过多,可以使用realloc() 增加循环内列表的大小。如果strdup不可用,可以使用malloc()strlen()函数轻松实现。

为简单起见,我排除了错误检查。理想情况下,应该检查 malloc()strdup 是否有错误。

【讨论】:

猜你喜欢
  • 2017-02-25
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多