系统调用----sandir
scandir函数:读取特定的目录数据
表头文件:#include
定义函数:int scandir(const char *dir, struct dirent **namelist, nt (*select) (const struct dirent *), nt
(*compar) (const struct dirent **, const struct dirent**));
函数说明:
scandir()会扫描参数dir指定的目录文件,经由参数select指定的函数来挑选目录结构至参数namelist数组中,最后再调用
参数compar指定的函数来排序namelist 数组中的目录数据。每次从目录文件中读取一个目录结构后便将此结构传给参数select所指的
函数, select函数若不想要将此目录结构复制到namelist数组就返回0,若select为空指针则代表选择所有的目录结构。scandir()会
调用 qsort()来排序数据,参数compar则为qsort()的参数,若是要排列目录名称字母则可使用alphasort(). 结构dirent定义请参考
readdir()
返回值 :成功则返回复制到namelist数组中的数据结构数目,有错误发生则返回-1
错误代码:ENOMEM 核心内存不足
Example
/* print files in current directory in reverse order */
#include
main()
{
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, alphasort);
if (n d_name);
free(namelist[n]);
}
free(namelist);
}
}
2:=========================================================
代码如下:
#include <dirent.h>
#include <stdio.h>
// 自定义的过滤器函数
// 本函数只能做为 scandir 函数的参数,被 scandir 函数回调
// 注:1. 本函数不能被定义为 static int custom_filter( const struct dirent *pSDirent ) 静态函数;
// 2. 参数一定要为 const struct dirent *pSDirent 不可省略前面的 const ;
int
custom_filter( const struct dirent *dp )
{
if ( 0 == strncmp( "get",
dp->d_name,
3 ) )
{
// 非零为保留 - 不滤掉
return 1;
}
// 零为过滤掉 - 过滤掉
return 0;
}
void main()
{
struct dirent **namelist;
int i;
int total;
total = scandir( "./",
&namelist,
custom_filter,
alphasort );
if ( total < 0 )
{
printf( "scandir fault\n" );
}
else
{
for( i = 0;
i < total;
i++ )
{
printf( "%s\n",
namelist[i]->d_name );
}
printf( "total = %d\n",
total );
}
}
结果如下:
不加 custom_filter 过滤函数的情况
total = scandir( "./",
&namelist,
0,
alphasort );
wzhnsc@wzhnsc-Ubuntu:/media/TestSrc$ ./getselfname
.
..
getselfname
getselfname.c
total = 4
加了 custom_filter 过滤函数的情况
total = scandir( "./",
&namelist,
custom_filter,
alphasort );
wzhnsc@wzhnsc-Ubuntu:/media/TestSrc$ ./getselfname
getselfname
getselfname.c
total = 2
系统调用----sandir相关推荐
- 实验四:使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
贺邦+原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验目的: 使用库函数 ...
- 专题8-Linux系统调用
1.从用户空间到内核空间的时候需要进行系统调用 2.系统调用通过swi指令实现,这个时候会有一个参数,存放到r7寄存器,用来指定要调用的内核态函数. 3.一般内核态的实际函数可以再任意的.c文件实现, ...
- linux c 启动程序吗,Linux下C程序启动时的系统调用
写程序跟踪发现,在Linux i386中,一个程序体完全为空的C语言程序启动时要进行近100个系统调用,如下所示. [ 1]syscall: 11 //execve [ 2]syscall: 45 / ...
- linux open系统调用的O_DIRECT标记
前言 open系统调用中针对打开的文件描述符,可以增加一个O_DIRECT标记,该标记能够使得针对该文件描述符的写操作绕过操作系统page cache,直接进入通用块设备层,从而减少页缓存对IO效率的 ...
- linux系统调用 ftruncate设置文件大小
系统调用ftruncate可以将一个文件裁剪为指定的大小,函数描述如下: 头文件:<unistd.h> <sys/types.h> 函数使用: int truncate(con ...
- linux 系统调用 read,write和lseek 使用
read系统调用 头文件 #include <unistd.h> 函数使用 ssize_t read(int fd, void *buf, size_t count) read 函数会从文 ...
- linux 系统调用 open函数使用
函数介绍 本文仅仅将open系统调用的使用简单总结一下,关于其实现原理大批的大佬分享可以自行学习.open系统调用主要用于打开或者创建一个文件,并返回文件描述符. 头文件 #include <f ...
- 汇编语言调用Linux系统调用
首先查找系统调用文件 #find / -name unistd.h /root/linux/include/unistd.h /usr/include/linux/unistd.h /usr/incl ...
- 程序员的自我修养--链接、装载与库笔记:系统调用与API
系统调用(System Call)是应用程序(运行库也是应用程序的一部分)与操作系统内核之间的接口,它决定了应用程序是如何与内核打交道的.无论程序是直接进行系统调用,还是通过运行库,最终还是会到达系统 ...
最新文章
- usbserials
- SAP UI5 应用开发教程之五十一 - 如何使用 Chrome 调试运行在手机上的 SAP UI5 Cordova 混合应用试读版
- PHP如何防采集方法代码
- C/C++程序员必读的十本书(上)
- centos7远程桌面端口_CentOS7启用远程桌面连接
- HTML small元素
- queue的常见用法
- python好东西啊
- R for data science 之 stringr包
- 深度学习框架_Caffe_Caffe框架入门简介
- android 小米键盘输入法切换,小米输入法键盘设置
- U盘中快捷方式病毒的解决方法
- 协议实例说明--租房
- 3D游戏编程与设计5——与游戏世界交互
- You are what you read 笔记
- 终身成长----“成长型思维”
- jquery获取span标签下的第一个span子标签内容
- MYSQL数据库跨服务器实时同步更新实践----文献阅读(污水管网水质预测)
- 如何访问原来Vue2官网?
- 数据库学习笔记2(补遗笔记1)