头文件:#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

struct linux_dirent64 {

u64d_ino; /* inode number 索引节点号 */

s64d_off; /* offset to this dirent 在目录文件中的偏移 */

unsigned shortd_reclen;/* length of this d_name 文件名长 */

unsigned chard_type;/* the type of d_name 文件类型 */

chard_name[0];/* file name (null-terminated) 文件名,最长255字符 */

};

测试代码如下:

example  1

#include

#include

#include

int main(void)

{

struct dirent **entry_list;

int count;

int i;

count = scandir("/home/book/workspace", &entry_list, 0, alphasort);

if (count < 0) {

perror("scandir");

return EXIT_FAILURE;

}

for (i = 0; i < count; i++) {

struct dirent *entry;

entry = entry_list[i];

printf("%s\n", entry->d_name);

free(entry);

}

free(entry_list);

return 0;

}

example  2 #define _GNU_SOURCE

#include

#include

#include

#include

#include

void make_test_data(char *path)

{

int i;

for (i = 0; i < 20; i++) {

char file[128];

sprintf(file, "file-%d", i);

creat(file, 0);

}

}

void scan(char *path, char *title, int (*sort)(const void *a, const void *b))

{

struct dirent **entry_list;

int count;

int i;

puts(title);

count = scandir(path, &entry_list, 0, sort);

if (count < 0) {

perror("scandir");

return EXIT_FAILURE;

}

for (i = 0; i < count; i++) {

struct dirent *entry;

entry = entry_list[i];

printf("%s\n", entry->d_name);

free(entry);

}

printf("\n");

free(entry_list);

}

int main(void)

{

char *path = ".";

make_test_data(path);

scan(path, "alphasort: ", alphasort);

scan(path, "versionsort: ", versionsort);

return 0;

}

example  3

#include

#include

#include

int filter(const struct dirent *entry)

{

return entry->d_name[0] == 'a';

}

int main(void)

{

struct dirent **entry_list;

int count;

int i;

count = scandir("/usr/include", &entry_list, filter, alphasort);

if (count < 0) {

perror("scandir");

return EXIT_FAILURE;

}

for (i = 0; i < count; i++) {

struct dirent *entry;

entry = entry_list[i];

printf("%s\n", entry->d_name);

free(entry);

}

free(entry_list);

return 0;

}

查看

entry->d_name是目录还文件参考这里:

点击这里!

linux的scandir函数如何排序,Linux c 目录操作函数scandir相关推荐

  1. python中和操作目录相关的函数包括_python文件和目录操作函数小结

    <python 与数据挖掘 > 一 导读 前 言为什么要写本书?Python是什么? Python是一种带有动态语义的.解释性的.面向对象的高级编程语言.其高级内置数据结构,结合动态类型和 ...

  2. Linux系统常用目录操作函数

    参考<Linux程序设计>第二版P103 扫描目录: #include <stdio.h> #include <stdlib.h> #include <str ...

  3. Linux学习-文件IOA1——用结构体和文件操作函数实现文件的拷贝

    Linux学习-文件IOA1--用结构体和文件操作函数实现文件的拷贝 其实我们不必选用结构体去实现模仿拷贝功能的,但是为了锻炼我们的思维以及对结构体.文件操作函数的使用,所以我们就这样来折腾自己. 学 ...

  4. Linux 高并发学习笔记 - Linux 目录操作函数

    1.6.4 Linux 目录操作函数 Linux 高并发学习笔记 - 笔记索引 文章目录 1.6.4 Linux 目录操作函数 前言 切换工作目录 查看工作目录 创建目录 重命名目录 移除目录 遍历目 ...

  5. Hive常用函数(日期函数,取整函数,字符串操作函数,集合操作函数)

    常用函数 常用日期函数 常用取整函数 常用字符串操作函数 集合操作函数 多维分析 常用日期函数 unix_timestamp:返回当前或指定时间的时间戳 select unix_timestamp() ...

  6. 时间:2014年3月27日文件和目录操作函数

    主要内容: 目录操作:打开.读取.关闭.创建.重命名.删除 文件操作:打开.读.写.关闭.重命名.删除 信息获取函数 filemtime() filesize() file_exists()  is_ ...

  7. windows c语言目录操作函数,c语言目录操作在C/C++语言中如何进行目录操作,如得到目录内的 爱问知识人...

    这里给你提供一些C的目录操作函数,原型声明所在头文件为dir.h.dos.h,仅供参考: int chdir(char *path) 使指定的目录path(如:"C:\\WINDOWS&qu ...

  8. windows c语言目录操作函数,C/C++: C语言目录操作

    转自: http://hi.baidu.com/yinjiubo_java/blog/item/4b6a9e178706ec0dc83d6d4e.html 这里给你提供一些C的目录操作函数,原型声明所 ...

  9. Linux c 目录操作函数scandir

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

最新文章

  1. Struts1.x系列教程(5):HTML标签库
  2. matlab径向分布函数作图_常见的概率分布(matlab作图)
  3. 百度html在线编辑器插件,百度编辑器UEditor插件DjangoUeditor v1.8.143
  4. docker访问宿主机mysql_docker容器内访问宿主机127.0.0.1服务
  5. 【模板】高精度 [高精度]
  6. Linux操作Oracle(16)——Oracle扩容报错:ORA-01144_表空间数据文件超出最大限制
  7. Java网络编程(TCP协议-练习-上传文本文件)
  8. Win10 64位系统运行汇编程序(使用masm与dosbox)
  9. Linux搭建局域网邮箱服务器,菜鸟搭建开源的局域网邮件服务器-windows linux均适用...
  10. 服务器XP系统打印机共享设置,小编调解xp系统打印机共享设置和使用的详细教程...
  11. 注册表怎么禁用计算机,如何禁用注册表,注册表禁用和启动的方法
  12. ansys轴对称模型之二维模型
  13. 端元提取——逐次投影算法SPA与顶点成分分析VCA
  14. 2022-nc-Widespread increasing vegetation sensitivity to soil moisture
  15. 消防工程师 1.1 消防给水及设施(2)
  16. matlabnbsp;pcode命令nbsp;生成…
  17. 前端技术 | dva,美貌与智慧并存
  18. CSS3 设置模糊背景图片
  19. matlab二元不等式,大神们,求个解多元一次不等式的代码,要所有整数解
  20. git版本回退简单记录

热门文章

  1. Php目录结构解析,深入理解PHP之源码目录结构与功能说明
  2. 1. Qt Designer Studio界面介绍
  3. Python的活色生香
  4. python dtype(0)_Python numpy,创建数组,数据类型,dtype属性
  5. python学习随堂笔记—TCP服务端与客户端
  6. uniapp HBuilderX 无线连接手机调试
  7. 企业邮箱如何购买?企业邮箱费用哪家更划算?
  8. 扬帆起航,再踏征程(一)
  9. 华为p30的android版本,华为P30系列手机支不支持5G 华为P30系列手机有5G版本吗
  10. MySQL导入外部数据集