你如何在C目录列表? [英] How do you get a directory listing in C?

查看:119
本文介绍了你如何在C目录列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何扫描在C文件夹和文件的目录?它必须是跨平台​​的。

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

推荐答案

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

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

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

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

这篇关于你如何在C目录列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆