且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何列出一个C程序目录中的文件?

更新时间:2022-11-04 20:58:40

一个例子,可用于POSIX兼容的系统:

An example, available for POSIX compliant systems :

/*
 * This program displays the names of all files in the current directory.
 */

#include <dirent.h> 
#include <stdio.h> 

int main(void)
{
  DIR           *d;
  struct dirent *dir;
  d = opendir(".");
  if (d)
  {
    while ((dir = readdir(d)) != NULL)
    {
      printf("%s\n", dir->d_name);
    }

    closedir(d);
  }

  return(0);
}

要注意的是这种操作依赖于平台的温度。

Beware that such an operation is platform dependant in C.

来源:http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608