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

时间:2021-04-20 00:12:38

How do you scan a directory for folders and files in C? It needs to be cross-platform.

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

9 个解决方案

#1


69  

The following will print the names of the files in the current directory:

下面将打印当前目录中文件的名称:

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

(credit: http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html)

(来源:http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html)

#2


18  

The strict answer is "you can't", as the very concept of a folder is not truly cross-platform.

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

On MS platforms you can use _findfirst, _findnext and _findclose for a 'c' sort of feel, and FindFirstFile and FindNextFile for the underlying Win32 calls.

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

Here's the C-FAQ answer:

这是C-FAQ回答:

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

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

#3


13  

I've created an open source (BSD) C header that deals with this problem. It currently supports POSIX and Windows. Please check it out:

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

https://github.com/cxong/tinydir

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  

There is no standard C (or C++) way to enumerate files in a directory.

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

Under Windows you can use the FindFirstFile/FindNextFile functions to enumerate all entries in a directory. Under Linux/OSX use the opendir/readdir/closedir functions.

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

#5


7  

GLib is a portability/utility library for C which forms the basis of the GTK+ graphical toolkit. It can be used as a standalone library.

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

It contains portable wrappers for managing directories. See Glib File Utilities documentation for details.

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

Personally, I wouldn't even consider writing large amounts of C-code without something like GLib behind me. Portability is one thing, but it's also nice to get data structures, thread helpers, events, mainloops etc. for free

就我个人而言,我甚至不会考虑写大量的C-code而不写一些像GLib这样的东西。可移植性是一回事,但是免费获得数据结构、线程助手、事件、主线程等也是一件好事

Jikes, I'm almost starting to sound like a sales guy :) (don't worry, glib is open source (LGPL) and I'm not affiliated with it in any way)

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

#6


5  

opendir/readdir are POSIX. If POSIX is not enough for the portability you want to achieve, check Apache Portable Runtime

opendir / readdir POSIX。如果POSIX不足以实现所需的可移植性,请检查Apache Portable Runtime

#7


2  

Directory listing varies greatly according to the OS/platform under consideration. This is because, various Operating systems using their own internal system calls to achieve this.

根据正在考虑的操作系统/平台,目录列表有很大的不同。这是因为,各种操作系统使用它们自己的内部系统调用来实现这一点。

A solution to this problem would be to look for a library which masks this problem and portable. Unfortunately, there is no solution that works on all platforms flawlessly.

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

On POSIX compatible systems, you could use the library to achieve this using the code posted by Clayton (which is referenced originally from the Advanced Programming under UNIX book by W. Richard Stevens). this solution will work under *NIX systems and would also work on Windows if you have Cygwin installed.

在POSIX兼容系统上,您可以使用库来实现这一点,使用Clayton发布的代码(这一代码最初来自W. Richard Stevens在UNIX book下的高级编程)。这个解决方案将在*NIX系统下工作,如果安装了Cygwin,也可以在Windows上运行。

Alternatively, you could write a code to detect the underlying OS and then call the appropriate directory listing function which would hold the 'proper' way of listing the directory structure under that OS.

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

#8


1  

The most similar method to readdir is probably using the little-known _find family of functions.

与readdir最相似的方法可能是使用鲜为人知的_find函数家族。

#9


-1  

You can find the sample code on the wikibooks link

您可以在wikibooks链接上找到示例代码

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

#1


69  

The following will print the names of the files in the current directory:

下面将打印当前目录中文件的名称:

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

(credit: http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html)

(来源:http://www.gnu.org/software/libtool/manual/libc/Simple-Directory-Lister.html)

#2


18  

The strict answer is "you can't", as the very concept of a folder is not truly cross-platform.

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

On MS platforms you can use _findfirst, _findnext and _findclose for a 'c' sort of feel, and FindFirstFile and FindNextFile for the underlying Win32 calls.

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

Here's the C-FAQ answer:

这是C-FAQ回答:

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

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

#3


13  

I've created an open source (BSD) C header that deals with this problem. It currently supports POSIX and Windows. Please check it out:

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

https://github.com/cxong/tinydir

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  

There is no standard C (or C++) way to enumerate files in a directory.

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

Under Windows you can use the FindFirstFile/FindNextFile functions to enumerate all entries in a directory. Under Linux/OSX use the opendir/readdir/closedir functions.

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

#5


7  

GLib is a portability/utility library for C which forms the basis of the GTK+ graphical toolkit. It can be used as a standalone library.

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

It contains portable wrappers for managing directories. See Glib File Utilities documentation for details.

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

Personally, I wouldn't even consider writing large amounts of C-code without something like GLib behind me. Portability is one thing, but it's also nice to get data structures, thread helpers, events, mainloops etc. for free

就我个人而言,我甚至不会考虑写大量的C-code而不写一些像GLib这样的东西。可移植性是一回事,但是免费获得数据结构、线程助手、事件、主线程等也是一件好事

Jikes, I'm almost starting to sound like a sales guy :) (don't worry, glib is open source (LGPL) and I'm not affiliated with it in any way)

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

#6


5  

opendir/readdir are POSIX. If POSIX is not enough for the portability you want to achieve, check Apache Portable Runtime

opendir / readdir POSIX。如果POSIX不足以实现所需的可移植性,请检查Apache Portable Runtime

#7


2  

Directory listing varies greatly according to the OS/platform under consideration. This is because, various Operating systems using their own internal system calls to achieve this.

根据正在考虑的操作系统/平台,目录列表有很大的不同。这是因为,各种操作系统使用它们自己的内部系统调用来实现这一点。

A solution to this problem would be to look for a library which masks this problem and portable. Unfortunately, there is no solution that works on all platforms flawlessly.

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

On POSIX compatible systems, you could use the library to achieve this using the code posted by Clayton (which is referenced originally from the Advanced Programming under UNIX book by W. Richard Stevens). this solution will work under *NIX systems and would also work on Windows if you have Cygwin installed.

在POSIX兼容系统上,您可以使用库来实现这一点,使用Clayton发布的代码(这一代码最初来自W. Richard Stevens在UNIX book下的高级编程)。这个解决方案将在*NIX系统下工作,如果安装了Cygwin,也可以在Windows上运行。

Alternatively, you could write a code to detect the underlying OS and then call the appropriate directory listing function which would hold the 'proper' way of listing the directory structure under that OS.

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

#8


1  

The most similar method to readdir is probably using the little-known _find family of functions.

与readdir最相似的方法可能是使用鲜为人知的_find函数家族。

#9


-1  

You can find the sample code on the wikibooks link

您可以在wikibooks链接上找到示例代码

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