() 头文件
#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. Web安全系列(三):XSS 攻击进阶(挖掘漏洞)

    前言 在前些章节 (web安全系列(一):XSS 攻击基础及原理)以及(Web安全系列(二):XSS 攻击进阶(初探 XSS Payload))中,我详细介绍了 XSS 形成的原理以及 XSS 攻击的 ...

  2. No bean named &#39;cxf&#39; is defined

    1.错误描写叙述  严重:Exception starting filter CXFServlet        org.springframework.beans.factory.NoSuchBea ...

  3. Snubber电路

    http://www.elecfans.com/yuanqijian/dianrongqi/20170601520736.html https://wenku.baidu.com/view/166f1 ...

  4. HDU BestCoder Round #1 1002 项目管理

    项目管理 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. 机器学习实战之SVM

    一引言: 支持向量机这部分确实很多,想要真正的去理解它,不仅仅知道理论,还要进行相关的代码编写和测试,二者想和结合,才能更好的帮助我们理解SVM这一非常优秀的分类算法 支持向量机是一种二类分类算法,假 ...

  6. Ubuntu64位安装Adobe Reader 9.5.5

    Aodbe Reader在Linux下的效果比Foxit Reader(福昕阅读器)要好一些,尤其对于中文文档而言.本文介绍Adobe Reader在Ubuntu下的安装,文章<Ubuntu12 ...

  7. &lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. PHP进阶知识

    关于PHP程序员技术职业生涯规划:http://rango.swoole.com/ Micro Service Framework For PHP:https://github.com/pinguo/ ...

  9. 【BZOJ4197】[Noi2015]寿司晚宴 状压DP+分解质因数

    [BZOJ4197][Noi2015]寿司晚宴 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴 ...

  10. 九度OJ 1039:Zero-complexity Transposition(逆置) (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3093 解决:1255 题目描述: You are given a sequence of integer numbers. Zero-co ...