linux的scandir函数如何排序,Linux c 目录操作函数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
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相关推荐
- python中和操作目录相关的函数包括_python文件和目录操作函数小结
<python 与数据挖掘 > 一 导读 前 言为什么要写本书?Python是什么? Python是一种带有动态语义的.解释性的.面向对象的高级编程语言.其高级内置数据结构,结合动态类型和 ...
- Linux系统常用目录操作函数
参考<Linux程序设计>第二版P103 扫描目录: #include <stdio.h> #include <stdlib.h> #include <str ...
- Linux学习-文件IOA1——用结构体和文件操作函数实现文件的拷贝
Linux学习-文件IOA1--用结构体和文件操作函数实现文件的拷贝 其实我们不必选用结构体去实现模仿拷贝功能的,但是为了锻炼我们的思维以及对结构体.文件操作函数的使用,所以我们就这样来折腾自己. 学 ...
- Linux 高并发学习笔记 - Linux 目录操作函数
1.6.4 Linux 目录操作函数 Linux 高并发学习笔记 - 笔记索引 文章目录 1.6.4 Linux 目录操作函数 前言 切换工作目录 查看工作目录 创建目录 重命名目录 移除目录 遍历目 ...
- Hive常用函数(日期函数,取整函数,字符串操作函数,集合操作函数)
常用函数 常用日期函数 常用取整函数 常用字符串操作函数 集合操作函数 多维分析 常用日期函数 unix_timestamp:返回当前或指定时间的时间戳 select unix_timestamp() ...
- 时间:2014年3月27日文件和目录操作函数
主要内容: 目录操作:打开.读取.关闭.创建.重命名.删除 文件操作:打开.读.写.关闭.重命名.删除 信息获取函数 filemtime() filesize() file_exists() is_ ...
- windows c语言目录操作函数,c语言目录操作在C/C++语言中如何进行目录操作,如得到目录内的 爱问知识人...
这里给你提供一些C的目录操作函数,原型声明所在头文件为dir.h.dos.h,仅供参考: int chdir(char *path) 使指定的目录path(如:"C:\\WINDOWS&qu ...
- windows c语言目录操作函数,C/C++: C语言目录操作
转自: http://hi.baidu.com/yinjiubo_java/blog/item/4b6a9e178706ec0dc83d6d4e.html 这里给你提供一些C的目录操作函数,原型声明所 ...
- Linux c 目录操作函数scandir
头文件:#include <dirent.h> 定义函数:int scandir(const char *dir, struct dirent **namelist, nt (*sel ...
最新文章
- Struts1.x系列教程(5):HTML标签库
- matlab径向分布函数作图_常见的概率分布(matlab作图)
- 百度html在线编辑器插件,百度编辑器UEditor插件DjangoUeditor v1.8.143
- docker访问宿主机mysql_docker容器内访问宿主机127.0.0.1服务
- 【模板】高精度 [高精度]
- Linux操作Oracle(16)——Oracle扩容报错:ORA-01144_表空间数据文件超出最大限制
- Java网络编程(TCP协议-练习-上传文本文件)
- Win10 64位系统运行汇编程序(使用masm与dosbox)
- Linux搭建局域网邮箱服务器,菜鸟搭建开源的局域网邮件服务器-windows linux均适用...
- 服务器XP系统打印机共享设置,小编调解xp系统打印机共享设置和使用的详细教程...
- 注册表怎么禁用计算机,如何禁用注册表,注册表禁用和启动的方法
- ansys轴对称模型之二维模型
- 端元提取——逐次投影算法SPA与顶点成分分析VCA
- 2022-nc-Widespread increasing vegetation sensitivity to soil moisture
- 消防工程师 1.1 消防给水及设施(2)
- matlabnbsp;pcode命令nbsp;生成…
- 前端技术 | dva,美貌与智慧并存
- CSS3 设置模糊背景图片
- matlab二元不等式,大神们,求个解多元一次不等式的代码,要所有整数解
- git版本回退简单记录