获取指定目录下的文件列表
来源:互联网 发布:环境地质数据包括哪些 编辑:程序博客网 时间:2024/05/18 03:06
//获取指定目录下的所有文件列表 author:wangchangshaui jlu
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
while ((ent = readdir(pDir)) != NULL)
{ //统计当前文件夹下有多少文件(不包括文件夹)
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//关闭目录
closedir(pDir);
// myLog("共%d个文件\n", count);
//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d个文件:%s\n", i, ent->d_name);
i++;
}
} //for
//关闭目录
closedir(pDir);
*fileCount = count;
return fileNameList;
}
char** getFileNameArray(const char *path, int* fileCount)
{
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
while ((ent = readdir(pDir)) != NULL)
{ //统计当前文件夹下有多少文件(不包括文件夹)
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
count++;
}
} //while
//关闭目录
closedir(pDir);
// myLog("共%d个文件\n", count);
//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
//打开目录
if ((pDir = opendir(path)) == NULL)
{
myLog("Cannot open directory:%s\n", path);
return NULL;
}
//读取目录
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
{
if (strlen(ent->d_name) <= 0)
{
continue;
}
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
{
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
{
myLog("Malloc heap failed!\n");
return NULL;
}
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d个文件:%s\n", i, ent->d_name);
i++;
}
} //for
//关闭目录
closedir(pDir);
*fileCount = count;
return fileNameList;
}
0 0
- 获取指定目录下的文件列表
- C语言 获取指定目录下的文件列表
- 获取指定目录下的文件名列表
- 获取指定目录下的所有文件
- Go 获取指定目录下的文件
- 获取指定目录下的文件
- 获取指定目录下的文件
- 获取指定目录下的文件
- 获取指定目录下的文件
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- C++获取目录下的文件列表
- 导出指定目录下的文件列表包括子文件
- 获取指定目录下的指定类型的文件
- java获取指定目录下所有指定扩展名的文件
- Java--获取指定目录下指定suffix的文件
- 获取指定目录下,指定扩展名的文件
- warning:deprecated conversion from string constant to 'char *'
- 【FreeMarker】【程序开发】在Servlet中使用FreeMarker
- YII MVC之用户注册和用户登录(一)
- 通过管道实现文件复制
- 高效面试之栈
- 获取指定目录下的文件列表
- 实现所有文件格式复制
- md5加密
- LeetCode刷题笔录LRUCache
- 与Scheme共舞
- 高效面试之数组专题中级篇(leetcode)
- 多线程实现文件拷贝
- 白话经典算法系列之三 希尔排序的实现
- 竞价点击服务器和网站SEO优化服务器整合开发流程