在Unix/Linux系统中,要获取一个指定目录下所有的文件或文件夹,一般用dirent.h(POSIX标准定义的目录操作头文件)。

一、数据类型

在头文件<dirent.h>中定义了两种主要的数据类型。

DIR:代表一个目录流的结构体。

struct __dirstream
{
void *__fd; /* 'struct hurd_fd' pointer for descriptor.*/
char *__data; /* Directory block. */
int __entry_data; /* Entry number `__data' corresponds to.*/
char *__ptr; /* Current pointer into the block.*/
int __entry_ptr; /* Entry number `__ptr' corresponds to.*/
size_t __allocation; /* Space allocated for the block.*/
size_t __size; /* Total valid data in the block.*/
__libc_lock_define (, __lock) /* Mutex lock for this structure.*/
}; typedef struct __dirstream DIR;

struct dirent:包含一个文件或目录信息的结构体。

struct dirent
{
long d_ino; /* inode number 索引节点号 */
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
unsigned short d_reclen; /* length of this d_name 文件名长 */
unsigned char d_type; /* the type of d_name 文件类型 */
char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}

二、函数原型

DIR* opendir(const char* dirname);
/* 打开一个目录:
成功 - 返回指向DIR类型对象的指针。
失败 - 返回NULL */ int closedir(DIR *dirp);
/* 关闭目录流:
成功 - 返回0
失败 - 返回-1 */ struct dirent *readdir(DIR *dirp);
/* 读取目录流:
成功 - 返回指向struct dirent对象的指针,当前位置向后移。
失败 - 返回NULL(出错或流末尾) */ int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
/* 读取目录流:用 dirp 当前位置的目录项初始化entry,并让 result 指向 entry。
成功 - 返回0
失败 - 返回error number */ void rewinddir(DIR *dirp);
/* 重置目录流的位置到开头 */ void seekdir(DIR *dirp, long int loc);
/* 设置目录流的位置,设置以后readdir()会读取到loc位置的目录项。 */ long int telldir(DIR *dirp);
/* 返回目录流的当前位置 */

三、示例代码

下面是一段 C 代码,输出指定目录下的所有文件或目录名:

#include<stdio.h>
#include<dirent.h> int main()
{
DIR *dp;
struct dirent *dirp;
char dirname[256]; printf("Please input a directory: ");
scanf("%s",dirname);
if((dp = opendir(dirname)) == NULL)
printf("Can't open %s\n", dirname); while((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name); closedir(dp);
return 0;
}

C++代码:

#include<iostream>
#include<string>
#include<dirent.h>
using namespace std; int main()
{
string dirname;
DIR *dp;
struct dirent *dirp; cout << "Please input a directory: ";
cin >> dirname;
if((dp = opendir(dirname.c_str())) == NULL)
cout << "Can't open " << dirname << endl; while((dirp = readdir(dp)) != NULL)
cout << dirp->d_name << endl; closedir(dp);
return 0;
}

有些情况下,我们只要输出文件而不需要文件夹(目录),这时可以通过dirent结构体中的d_type进行过滤。d_type表示类型,4表示目录,8表示普通文件,0表示未知设备

while((dirp = readdir(dp)) != NULL)
if(dirp->d_type == 8) // 只输出文件名,不输出目录名
cout << dirp->d_name << endl;

如果需要查找指定类型(特定后缀)的文件,可以使用C++11的正则表达式进行匹配:

// #include<regex>
regex reg_obj(".*\.doc", regex::icase);
while((dirp = readdir(dp)) != NULL)
if(regex_match(dirp->d_name, reg_obj)) // regex_match()匹配
cout << dirp->d_name << endl;

另外,Unix/linux下提供了POSIX标准的正则库regex.h

个人站点:http://songlee24.github.com

【Unix编程】C/C++获取目录下文件或目录的更多相关文章

  1. Centos-显示目录或者目录下文件信息-ls

    ls 显示指定目录信息或指定目录下文件和目录信息,后边不跟文件目录路径信息默认为当前工作目录 默认显示输出信息的总行数统计数 相关参数 -a 显示所有文件或子目录,包含隐藏文档 # linux中以 . ...

  2. python生成器 获取 目录下文件

    # os.walk()和os.list 都是得到所有文件的列表, 如果目录下文件特别多, 上亿了, 我们就需要生成器的方式获取 # 要求目录下面没有目录, 会递归到子目录下面找文件, (如果有子目录可 ...

  3. IO流-获取指定目录下文件夹和文件对象【File类】

    一.运用File类实现获取指定目录下文件夹和文件对象 1.File类 2.方法: 获取文件绝对路径 :getAbsolutePath 案例: import java.io.File; /** * 获取 ...

  4. C# 获取目录下文件

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. Linux中/proc目录下文件详解

    转载于:http://blog.chinaunix.net/uid-10449864-id-2956854.html Linux中/proc目录下文件详解(一)/proc文件系统下的多种文件提供的系统 ...

  6. Linux中/proc目录下文件详解(转贴)

      转载:http://www.sudu.cn/info/index.php?op=article&id=302529   Linux中/proc目录下文件详解(一) 声明:可以自由转载本文, ...

  7. python 获取当前目录下文件(转)

    今天继续整理原来写的 python 代码,下面是获取文件信息的 python 处理代码. 获取指定目录下文件的文件名以及文件的数量,然后列出其中还存在的目录名称: #!/usr/bin/env pyt ...

  8. java 获取classpath下文件多种方式

    java 获取classpath下文件多种方式 一:properties下配置 在resources下定义server.properties register.jks.path=classpath\: ...

  9. linux获得目录下文件个数

    获得当前目录下文件个数赋值给变量panonum: panonum=$(ls -l |grep "^-" | wc -l) 获取指定目录下文件个数赋值给指定变量: panonum=$ ...

随机推荐

  1. Python2和Python3除法

    Python2和Python3除法   Python2除法:/,//,% "/":整数相除,向下取整:浮点数相除,结果包含小数(类似1/2,想保留小数应该写成1.0/2或者1*1. ...

  2. 通过 GCC 学习 OpenMP 框架

     OpenMP 框架是使用 C.C++ 和 Fortran 进行并发编程的一种强大方法.GNU Compiler Collection (GCC) V4.4.7 支持 OpenMP 3.0 标准,而 ...

  3. CAD参数绘制椭圆(网页版)

    在CAD设计时,需要绘制椭圆,用户可以设置椭圆的基本属性. 主要用到函数说明: _DMxDrawX::DrawEllipse 绘制椭圆.详细说明如下: 参数 说明 DOUBLE dCenterX 椭圆 ...

  4. 二、spring中装配bean

    在spring框架中提供了三种 bean的装配方式,当然这三种装配方式是可以灵活的进行组合使用的,项目中使用最多的是自动装配bean的方式,也就是通过注解的方式进行bean的装配,一下是四种装配方式的 ...

  5. vue 中动画配置

    <transition name="fade">   <router-view ></router-view> </transition& ...

  6. iphone X 的适配

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. mysql主从同步,主库宕机解决方案

    链接:https://blog.csdn.net/zfl589778/article/details/51441719

  8. Go:面向"对象"

    一.封装 封装的实现步骤: 将结构体.字段的首字母小写(不能被导出): 给结构体所在的包提供一个工厂模式的函数,首字母大写.类似一个构造函数: 提供一个首字母大写的方法,由于获取结构体属性的值. 二. ...

  9. win7系统上VMware虚拟机安装linux7.2上网配置

    环境: 本机是window7系统,安装VMware虚拟机,在VMware安装了Rdhat系统,想上网,在网上搜索了不少的配置方法,这篇文章介绍的比较全面,感谢分享,摘抄在这里让更多的爱好者学习.我自己 ...

  10. python字符串,常用编码

    Python的字符串和编码 1.常用编码 与python有关的编码主要有:ASCII.Unicode.UTF-8 其中ASCII如今可以视作UTF-8的子集 内存中统一使用Unicode编码(如记事本 ...