() 头文件
#include <dirent.h>
() 函数定义
int scandir(const char *dir,struct dirent **namelist,int (*filter)(const void *b),
int ( * compare )( const struct dirent **, const struct dirent ** ) );
int alphasort(const void *a, const void *b);
int versionsort(const void *a, const void *b); () 使用说明 函数scandir扫描dir目录下以及dir子目录下满足filter过滤模式的文件,返回的结果是compare函数经过排序的,并保存在 namelist中。注意namelist是通过malloc动态分配内存的,所以在使用时要注意释放内存。alphasort和versionsort 是使用到的两种排序的函数。
当函数成功执行时返回找到匹配模式文件的个数,如果失败将返回-。
() 使用示例 /*
scandir函数
*/
#include <stdio.h>
#include <dirent.h>
int customFilter(const struct dirent *pDir)
{
if (strncmp("test", pDir->d_name, ) ==
&& pDir->d_type & 0x04
&& strcmp(pDir->d_name, ".")
&& strcmp(pDir->d_name, ".."))
{
return ;
}
return ;
}
int main()
{
struct dirent **namelist;
int n;
int i;
n = scandir(".", &namelist, customFilter, alphasort);
if (n < )
{
perror("scandir");
}
else
{
for (i = ; i < n; i++)
{
printf("%s\n", namelist[i]->d_name);
free(namelist[i]);
}
free(namelist);
}
}

linux C++ scandir 的使用的更多相关文章

  1. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  2. 【app】遍历目录所有文件

    遍历目录所有文件   原创,转载时请注明,谢谢.邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...

  3. linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体[转]

    linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体 1.引用头文件#include<dirent.h> struct ...

  4. Linux c 目录操作函数scandir

    头文件#include <dirent.h>  函数定义:int scandir(const char *dir,struct dirent **namelist,int (*filter ...

  5. Linux服务器上安装织梦CMS

    安装篇 第一步:配置防火墙(默认情况下,端口80和3306是拒绝访问的,在防火墙上进行配置): vi /etc/sysconfig/iptables(在"COMMIT"的上一行加上 ...

  6. Linux下指定版本编译安装LAMP

    说明: 操作系统:CentOS 6.5 64位 需求: 编译安装LAMP运行环境 各软件版本如下: MySQL:mysql-5.1.73 Apache:httpd-2.2.31 PHP:php-5.2 ...

  7. linux查找webshell

    原文出处:http://my.oschina.net/longquan/blog/155905 首先认识一下小马,一般大马容易暴露,骇客都会留一手,把小马加入正常PHP文件里面 <?php ev ...

  8. linux 操作系统下c语言编程入门

    2)Linux程序设计入门--进程介绍 3)Linux程序设计入门--文件操作 4)Linux程序设计入门--时间概念 5)Linux程序设计入门--信号处理 6)Linux程序设计入门--消息管理  ...

  9. Centos下安装配置LAMP(Linux+Apache+MySQL+PHP)

    Centos下安装配置LAMP(Linux+Apache+MySQL+PHP)   关于LAMP的各种知识,还请大家自行百度谷歌,在这里就不详细的介绍了,今天主要是介绍一下在Centos下安装,搭建一 ...

随机推荐

  1. 抽象类(abstract)和接口(interface)的区别

    1 抽象类是不能被实例化的类,只能作为由其他类继承的基类:    接口则定义了实现某种服务的一般规范(Objective-C中将接口称为“协议”(protocol)),声明了必需的函数和常量,但不指定 ...

  2. fabric使用实例(发布web包的一个例子)

    #!/usr/bin/env python # -*- coding: utf-8 -* #添加中文注释的编码 #fabfile.py from fabric.api import * env.use ...

  3. RF--- selenium

  4. Codeforces Hello2015第一题Cursed Query

    英文题面: De Prezer loves movies and series. He has watched the Troy for like 100 times and also he is a ...

  5. ARM内核和架构

    转:深入浅谈,CPU设计原理          CPU的内部架构和工作原理 推荐一本书:编码的奥秘 一.ARM内核和架构 ARM产品越来越丰富,命名也越来越多.很多朋友提问: ARM内核和架构都是什么 ...

  6. java mysql自定义函数UDF之调用c函数

    正如sqlite可以定义自定义函数,它是通过API定义c函数的,不像其他,如这里的mysql.sqlite提供原生接口就可以方便的调用其他语言的方法,同样的mysql也支持调用其它语言的方法. goo ...

  7. mysql日期格式转化

    select DATE_FORMAT( '20170701', '%Y-%m-%d'); 先挖坑

  8. Mysql 免密码登录,修改密码及忘记密码操作

    ----免密码登陆 方式一 my.cnf增加[client]标签 [client] user="root" password="你的密码" 单对定义不同的客户端 ...

  9. 16 redis之sentinel运维监控

    一:sentinel运维监控 Sentinel不断与master通信,获取master的slave信息. 监听master与slave的状态 如果某slave失效,直接通知master去除该slave ...

  10. centOS7 安装nginx+php+mysql

    nginx安装 本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automa ...