如何在 C 中获得目录列表?

Posted

技术标签:

【中文标题】如何在 C 中获得目录列表?【英文标题】:How do you get a directory listing in C? 【发布时间】:2010-09-05 23:08:22 【问题描述】:

如何在 C 中扫描目录中的文件夹和文件?它需要是跨平台的。

【问题讨论】:

【参考方案1】:

以下 POSIX 程序将打印当前目录中文件的名称:

#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main (void)

  DIR *dp;
  struct dirent *ep;     
  dp = opendir ("./");

  if (dp != NULL)
  
    while (ep = readdir (dp))
      puts (ep->d_name);

    (void) closedir (dp);
  
  else
    perror ("Couldn't open the directory");

  return 0;

信用:http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html

在 Ubuntu 16.04 中测试。

【讨论】:

这个怎么样:error: unknown type name 'off64_t' 我不得不将while 条件更改为(ep = readdir(dp)) != NULL) @Clayton - 如果我只想打印 .txt 文件怎么办? 对于 Windows,您可能需要 dirent.h 的 win32 实现:github.com/tronkko/dirent。只需将 更改为 "dirent.h"【参考方案2】:

严格的答案是“你不能”,因为文件夹的概念并不是真正的跨平台。

在 MS 平台上,您可以使用 _findfirst、_findnext 和 _findclose 获得“c”类感觉,并使用 FindFirstFile 和 FindNextFile 进行底层 Win32 调用。

这是 C-FAQ 的答案:

http://c-faq.com/osdep/readdir.html

【讨论】:

是否已经在某处为 _findfirst、_findnext 和 _findclose 设置了别名?不在 windows.h 中,对吧?【参考方案3】:

我创建了一个开源 (BSD) C 头文件来处理这个问题。它目前支持 POSIX 和 Windows。请查看:

https://github.com/cxong/tinydir

tinydir_dir dir;
tinydir_open(&dir, "/path/to/dir");

while (dir.has_next)

    tinydir_file file;
    tinydir_readfile(&dir, &file);

    printf("%s", file.name);
    if (file.is_dir)
    
        printf("/");
    
    printf("\n");

    tinydir_next(&dir);


tinydir_close(&dir);

【讨论】:

这比原来的系统相关实现有什么帮助?还是您只需要一个用于 Windows 和 POSIX 的命名库? @kevr: tinydir.h 在一个方便的头文件中是一个巨大的#ifdef 解决方案。【参考方案4】:

没有标准的 C(或 C++)方法来枚举目录中的文件。

在 Windows 下,您可以使用 FindFirstFile/FindNextFile 函数来枚举目录中的所有条目。在 Linux/OSX 下使用 opendir/readdir/closedir 函数。

【讨论】:

【参考方案5】:

GLib 是 C 的可移植性/实用程序库,它构成了 GTK+ 图形工具包的基础。它可以作为一个独立的库使用。

它包含用于管理目录的可移植包装器。有关详细信息,请参阅Glib File Utilities 文档。

就我个人而言,如果没有像 GLib 这样的东西,我什至不会考虑编写大量 C 代码。可移植性是一回事,但免费获得数据结构、线程助手、事件、主循环等也很好

Jikes,我几乎开始听起来像个销售员了 :)(别担心,glib 是开源的 (LGPL),我与它没有任何关系)

【讨论】:

【参考方案6】:

opendir/readdir 是 POSIX。如果 POSIX 不足以实现您想要实现的可移植性,请查看Apache Portable Runtime

【讨论】:

【参考方案7】:

目录列表因所考虑的操作系统/平台而异。这是因为,各种操作系统都使用自己的内部系统调用来实现这一点。

解决这个问题的方法是寻找一个可以掩盖这个问题并且可移植的库。不幸的是,没有可以在所有平台上完美运行的解决方案。

在与 POSIX 兼容的系统上,您可以使用 Clayton 发布的代码(最初引用自 W. Richard Stevens 的《UNIX 下的高级编程》一书)使用该库来实现此目的。此解决方案可以在 *NIX 系统下运行,如果您安装了 Cygwin,也可以在 Windows 上运行。

或者,您可以编写代码来检测底层操作系统,然后调用适当的目录列表函数,该函数将保持列出该操作系统下目录结构的“正确”方式。

【讨论】:

【参考方案8】:

readdir相似的方法可能是使用鲜为人知的_find family of functions。

【讨论】:

【参考方案9】:

您可以在wikibooks link找到示例代码

/**************************************************************
 * A simpler and shorter implementation of ls(1)
 * ls(1) is very similar to the DIR command on DOS and Windows.
 **************************************************************/
#include <stdio.h>
#include <dirent.h>

int listdir(const char *path) 

  struct dirent *entry;
  DIR *dp;

  dp = opendir(path);
  if (dp == NULL) 
  
    perror("opendir");
    return -1;
  

  while((entry = readdir(dp)))
    puts(entry->d_name);

  closedir(dp);
  return 0;


int main(int argc, char **argv) 
  int counter = 1;

  if (argc == 1)
    listdir(".");

  while (++counter <= argc) 
    printf("\nListing %s...\n", argv[counter-1]);
    listdir(argv[counter-1]);
  

  return 0;

【讨论】:

以上是关于如何在 C 中获得目录列表?的主要内容,如果未能解决你的问题,请参考以下文章

如何在C#中获得组合框的所有选定值

Java中如何获得某个磁盘(根目录)?比如C盘

如何获得使用Dir.glob只有文件的列表?

如何通过列表理解检索字典的键列表和值?

C#WPF如何在由数据模板中的对象列表组成的列表框中设置项目[重复]

如何使用ls,grep和wc生成/ usr / bin目录中所有文件的列表,其大小至少为10,000,000字节?