簡體   English   中英

如何在 C 中獲得目錄列表?

[英]How do you get a directory listing in C?

如何在 C 中掃描目錄中的文件夾和文件? 它需要是跨平台的。

以下 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)) != NULL)
      puts (ep->d_name);
          
    (void) closedir (dp);
    return 0;
  }
  else
  {
    perror ("Couldn't open the directory");
    return -1;
  }
}

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

在 Ubuntu 16.04 中測試。

嚴格的答案是“你不能”,因為文件夾的概念並不是真正的跨平台。

在 MS 平台上,您可以使用 _findfirst、_findnext 和 _findclose 來獲得“c”類的感覺,並使用 FindFirstFile 和 FindNextFile 來獲得底層 Win32 調用。

這是 C-FAQ 的答案:

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

我創建了一個開源 (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);

沒有標准的 C(或 C++)方法來枚舉目錄中的文件。

在 Windows 下,您可以使用 FindFirstFile/FindNextFile 函數枚舉目錄中的所有條目。 在 Linux/OSX 下使用 opendir/readdir/closedir 函數。

GLib是 C 的可移植性/實用程序庫,它構成了 GTK+ 圖形工具包的基礎。 它可以用作一個獨立的庫。

它包含用於管理目錄的可移植包裝器。 有關詳細信息,請參閱Glib 文件實用程序文檔。

就個人而言,如果沒有像 GLib 這樣的東西,我什至不會考慮編寫大量的 C 代碼。 可移植性是一回事,但免費獲得數據結構、線程助手、事件、主循環等也很好

Jikes,我幾乎開始聽起來像個銷售員了 :) (別擔心,glib 是開源的(LGPL),我與它沒有任何關系)

opendir/readdir 是 POSIX。 如果 POSIX 不足以實現您想要實現的可移植性,請檢查Apache Portable Runtime

目錄列表根據所考慮的操作系統/平台而有很大差異。 這是因為,各種操作系統使用自己的內部系統調用來實現這一點。

這個問題的解決方案是尋找一個可以掩蓋這個問題並且可移植的庫。 不幸的是,沒有可以在所有平台上完美運行的解決方案。

在與 POSIX 兼容的系統上,您可以使用 Clayton 發布的代碼(最初引用自 W. Richard Stevens 的《UNIX 下的高級編程》一書)使用該庫來實現此目的。 此解決方案將在 *NIX 系統下運行,如果您安裝了 Cygwin,也可以在 Windows 上運行。

或者,您可以編寫代碼來檢測底層操作系統,然后調用適當的目錄列表函數,該函數將保持在該操作系統下列出目錄結構的“正確”方式。

readdir相似的方法可能是使用鮮為人知的_find系列函數

您可以在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;
}

很晚了,但我猜我不能忍受自己...

使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM