博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体[转]...
阅读量:6706 次
发布时间:2019-06-25

本文共 1380 字,大约阅读时间需要 4 分钟。

linux 系统获得当前文件夹下存在的所有文件 scandir函数和struct dirent **namelist结构体

1、引用头文件#include<dirent.h>

struct dirent **namelist;

int itotalfile;
itotalfile = scandir(filenameBuf,&namelist,0,alphasort);
char filenamelist[itotalfile][128];
for(int j = 0; j<itotalfile; j++)

{

          sprintf(filenamelist[j],"%s",filenameBuf) ;
          strcat(filenamelist[j],namelist[j]->d_name);

}

 2、

scandir函数:读取特定的目录数据

表头文件:#include <dirent.h>
定义函数:

int scandir(const char *dirp, struct dirent ***namelist,

          int (*filter)(const struct dirent *),

          int (*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

1 /* print files in current directory in reverse order */ 2 #include 
3 main() 4 { 5 struct dirent **namelist; 6 int n; 7 n = scandir(".", &namelist, 0, alphasort); 8 if (n < 0) 9 perror("scandir");10 else 11 {12 while(n--) 13 {14 printf("%s/n", namelist[n]->d_name);15 free(namelist[n]);16 }17 free(namelist);18 }

 

转载地址:http://ghflo.baihongyu.com/

你可能感兴趣的文章
从责任界定和问题预警角度 解读全栈溯源对DevOps的价值
查看>>
百度发布开源智能边缘计算平台OpenEdge
查看>>
JavaScript引擎V8 5.1遵循了更多的ECMAScript规范并支持WASM
查看>>
广度、深度、易用性,详解6大机器学习云
查看>>
雇佣和留住开发人员,打造优秀的团队
查看>>
关于5G被激烈讨论的那些争端和冲突
查看>>
Jenkins部署码云SpringBoot项目
查看>>
抛弃NVelocity,来玩玩Razor
查看>>
在JavaScript面向对象编程中使用继承(1)
查看>>
高铁与机场成交通信息化建设的双驾马车
查看>>
chmod命令
查看>>
货币的起源和职能是什么?绘制货币资金管理思维导图简单的方法介绍
查看>>
springboot+kafka+elk+docker+docker-compose+centos搭建日志收集系统
查看>>
时讯无线如何满足商业区的无线覆盖?
查看>>
2014最新open***搭建实例
查看>>
WinAPI: midiOutCachePatches - 预装音色
查看>>
finally执行顺序
查看>>
TWebBrowser 与 MSHTML(2): 获取 window 对象的时机
查看>>
【博客话题】IT人,你肿么了? ——除了IT,你还能选择什么?
查看>>
docker初步入门
查看>>