C语言alphasort()函数
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C语言alphasort()函数:依字母顺序排序目录结构
相关函数:scandir, qsort
头文件:#include
定义函数:int alphasort(const struct dirent **a, const struct dirent **b);
函数说明:alphasort()为scandir()最后调用qsort()函数时传给qsort()作为判断的函数, 详细说明请参考scandir()及qsort().
返回值参考qsort()。
范例
/* 读取/目录下所有的目录结构, 并依字母顺序排列*/
#include
main()
{
struct dirent **namelist;
int i, total;
total = scandir("/", &namelist, 0, alphasort);
if(total < 0)
perror("scandir");
{
for(i = 0; i < total; i++)
printf("%s\n", namelist[i]->d_name);
printf("total = %d\n", total);
}
}
执行:
..
.gnome
.gnome_private
ErrorLog
Weblog
bin
boot
dev
dosc
dosd
etc
home
lib
lost+found
mnt
opt
proc root sbin
tmp
usr
var
total = 24