yiffer的个人空间 https://blog.eetop.cn/edesign [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

C语言 获取指定目录下的文件列表

已有 2083 次阅读| 2017-3-5 10:14 |个人分类:C语言|系统分类:硬件设计

  1. //获取指定目录下的所有文件列表
  2. char** getFileNameArray(const char *path, int* fileCount)  
  3. {  
  4.     int count = 0;  
  5.     char **fileNameList = NULL;  
  6.     struct dirent* ent = NULL;  
  7.     DIR *pDir;  
  8.     char dir[512];  
  9.     struct stat statbuf;  
  10.   
  11. //打开目录  
  12.     if ((pDir = opendir(path)) == NULL)  
  13.     {  
  14.         myLog("Cannot open directory:%s\n", path);  
  15.         return NULL;  
  16.     }  
  17. //读取目录  
  18.     while ((ent = readdir(pDir)) != NULL)  
  19.     { //统计当前文件夹下有多少文件(不包括文件夹)  
  20. //得到读取文件的绝对路径名  
  21.         snprintf(dir, 512, "%s/%s", path, ent->d_name);  
  22.         //得到文件信息  
  23.         lstat(dir, &statbuf);  
  24.         //判断是目录还是文件  
  25.         if (!S_ISDIR(statbuf.st_mode))  
  26.         {  
  27.             count++;  
  28.         }  
  29.     } //while  
  30. //关闭目录  
  31.     closedir(pDir);  
  32. //  myLog("共%d个文件\n", count);  
  33.   
  34. //开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间  
  35.     if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)  
  36.     {  
  37.         myLog("Malloc heap failed!\n");  
  38.         return NULL;  
  39.     }  
  40.   
  41. //打开目录  
  42.     if ((pDir = opendir(path)) == NULL)  
  43.     {  
  44.         myLog("Cannot open directory:%s\n", path);  
  45.         return NULL;  
  46.     }  
  47. //读取目录  
  48.     int i;  
  49.     for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)  
  50.     {  
  51.         if (strlen(ent->d_name) <= 0)  
  52.         {  
  53.             continue;  
  54.         }  
  55.         //得到读取文件的绝对路径名  
  56.         snprintf(dir, 512, "%s/%s", path, ent->d_name);  
  57.         //得到文件信息  
  58.         lstat(dir, &statbuf);  
  59.         //判断是目录还是文件  
  60.         if (!S_ISDIR(statbuf.st_mode))  
  61.         {  
  62.             if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))  
  63.                     == NULL)  
  64.             {  
  65.                 myLog("Malloc heap failed!\n");  
  66.                 return NULL;  
  67.             }  
  68.             memset(fileNameList[i], 0, strlen(ent->d_name) + 1);  
  69.             strcpy(fileNameList[i], ent->d_name);  
  70.             myLog("第%d个文件:%s\n", i, ent->d_name);  
  71.             i++;  
  72.         }  
  73.     } //for  
  74. //关闭目录  
  75.     closedir(pDir);  
  76.   
  77.     *fileCount = count;  
  78.     return fileNameList;  

  79. 转自:http://blog.csdn.net/wangchangshuai0010/article/details/17454581

点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 3

    粉丝
  • 0

    好友
  • 20

    获赞
  • 69

    评论
  • 3705

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-3-29 17:53 , Processed in 0.015001 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部