代码之家  ›  专栏  ›  技术社区  ›  andrewrk

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

  •  52
  • andrewrk  · 技术社区  · 16 年前

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

    10 回复  |  直到 16 年前
        1
  •  72
  •   Ciro Santilli OurBigBook.com    6 年前

    以下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中测试。

        2
  •  19
  •   Jonathan Leffler    16 年前

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

    在MS平台上,您可以使用findfirst、findnext和findclose来获得“C”类型的感觉,使用findfirstfile和findnextfile来实现底层win32调用。

    以下是C-FAQ答案:

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

        3
  •  16
  •   congusbongus piRSquared    12 年前

    我已经创建了一个处理这个问题的开源(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);
    
        4
  •  9
  •   Andrew Grant    16 年前

    没有一个标准的C(或C++)枚举目录中的文件。

    在windows下,可以使用findfirstfile/findnextfile函数枚举目录中的所有条目。在linux/osx下使用opendir/readdir/closedir函数。

        5
  •  7
  •   Isak Savo    16 年前

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

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

    就我个人而言,如果没有像glib这样的东西在我身后,我甚至不会考虑编写大量的c代码。可移植性是一回事,但是免费获得数据结构、线程帮助程序、事件、主循环等也不错。

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

        6
  •  5
  •   Jonathan Leffler    16 年前

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

        7
  •  2
  •   Pascal    16 年前

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

    解决这个问题的方法是寻找一个隐藏这个问题并可移植的库。不幸的是,没有一个解决方案能在所有平台上完美地工作。

    在与posix兼容的系统上,您可以使用clayton发布的代码(它最初是从w.richard stevens的unix高级编程书籍中引用的)使用库来实现这一点。这个解决方案可以在*nix系统下运行,如果您安装了cygwin,它也可以在windows上运行。

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

        8
  •  1
  •   user541686    13 年前

    最多的 类似的 方法 readdir 可能是在用鲜为人知的 _find family of functions .

        9
  •  0
  •   Howard Wu    7 年前

    您可以在 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;
    }
    
        10
  •  0
  •   Andreas    6 年前

    太晚了,但我想我控制不住自己…

    使用C int system(char*) 你可以通过一个“便携式”的C接口来做各种各样的事情。您必须使用特定于系统的语言(例如shell/bash、dos、powershell)加载应用程序,但至少不需要链接额外的库。与之相反 dirent ,你可以用它做各种“平台无关”的事情。

    使用Linux列出目录的示例(大多是伪目录):

    system("ls . > some_file_or_pipe_or_whatever") //
    fscanf(file_or_pipe_or_whatever, "%s", a_line_from_ls);