沈阳惠天热电:Linux C :遍历输出指定目录下的所有文件
来源:百度文库 编辑:偶看新闻 时间:2024/05/03 00:44:15
在Linux下opendir()、readdir()和closedir()这三个函数主要用来遍历目录。在使用这三个函数前必须先包括以下两个头文件:#include #include
opendir函数的原型为:
DIR *opendir(const char *name);它返回一个DIR*类型,这就是一个句柄啦,你不用管它的内部结构是什么样的,只要知道这个句柄就是等一下要传给readdir()函数的参数就行了。
readdir函数的原型为:struct dirent *readdir(DIR *dir);看它的参数就知道该参数是opendir函数返回的句柄,而该函数的返回值是struct dirent* 类型,这里我们必须了解一下这个结构体:struct dirent { ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};这个结构体的d_name存放的就是文件的名字,这里的文件包括普通文件,目录文件等等,在linux的思想中,所有的东西都是文件。
closedir函数的原型为:int closedir(DIR *dir);这个函数就不用多说了,一般有开(open),就有关(close),这样的结构经常可出看到,如fopen,fclose等等。
三个函数介绍完了,直接来一个例子吧:
**********************************************SearchDir.c****************************************************
#include
#include
#include
#include
#include
char filename[256][256];
int len = 0;
int trave_dir(char* path, int depth)
{
DIR *d; //声明一个句柄
struct dirent *file; //readdir函数的返回值就存放在这个结构体中
struct stat sb;
if(!(d = opendir(path)))
{
printf("error opendir %s!!!\n",path);
return -1;
}
while((file = readdir(d)) != NULL)
{
//把当前目录.,上一级目录..及隐藏文件都去掉,避免死循环遍历目录
if(strncmp(file->d_name, ".", 1) == 0)
continue;
strcpy(filename[len++], file->d_name); //保存遍历到的文件名
//判断该文件是否是目录,及是否已搜索了三层,这里我定义只搜索了三层目录,太深就不搜了,省得搜出太多文件
if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth <= 3)
{
trave_dir(file->d_name, depth + 1);
}
}
closedir(d);
return 0;
}
int main()
{
int depth = 1;
int i;
trave_dir("/usr/keygoe/ini/", depth);
for(i = 0; i < len; i++)
{
printf("%s\t", filename[i]);
}
printf("\n");
return 0;
}
*****************************************************************************************************************
opendir函数的原型为:
DIR *opendir(const char *name);它返回一个DIR*类型,这就是一个句柄啦,你不用管它的内部结构是什么样的,只要知道这个句柄就是等一下要传给readdir()函数的参数就行了。
readdir函数的原型为:struct dirent *readdir(DIR *dir);看它的参数就知道该参数是opendir函数返回的句柄,而该函数的返回值是struct dirent* 类型,这里我们必须了解一下这个结构体:struct dirent { ino_t d_ino; /* inode number */
off_t d_off; /* offset to the next dirent */
unsigned short d_reclen; /* length of this record */
unsigned char d_type; /* type of file */
char d_name[256]; /* filename */
};这个结构体的d_name存放的就是文件的名字,这里的文件包括普通文件,目录文件等等,在linux的思想中,所有的东西都是文件。
closedir函数的原型为:int closedir(DIR *dir);这个函数就不用多说了,一般有开(open),就有关(close),这样的结构经常可出看到,如fopen,fclose等等。
三个函数介绍完了,直接来一个例子吧:
**********************************************SearchDir.c****************************************************
#include
#include
#include
#include
#include
char filename[256][256];
int len = 0;
int trave_dir(char* path, int depth)
{
DIR *d; //声明一个句柄
struct dirent *file; //readdir函数的返回值就存放在这个结构体中
struct stat sb;
if(!(d = opendir(path)))
{
printf("error opendir %s!!!\n",path);
return -1;
}
while((file = readdir(d)) != NULL)
{
//把当前目录.,上一级目录..及隐藏文件都去掉,避免死循环遍历目录
if(strncmp(file->d_name, ".", 1) == 0)
continue;
strcpy(filename[len++], file->d_name); //保存遍历到的文件名
//判断该文件是否是目录,及是否已搜索了三层,这里我定义只搜索了三层目录,太深就不搜了,省得搜出太多文件
if(stat(file->d_name, &sb) >= 0 && S_ISDIR(sb.st_mode) && depth <= 3)
{
trave_dir(file->d_name, depth + 1);
}
}
closedir(d);
return 0;
}
int main()
{
int depth = 1;
int i;
trave_dir("/usr/keygoe/ini/", depth);
for(i = 0; i < len; i++)
{
printf("%s\t", filename[i]);
}
printf("\n");
return 0;
}
*****************************************************************************************************************
linux下,如何列出目录下指定大小的文件?
在LINUX下如何将tar压缩文件解压到指定的目录下
求.Net遍历指定文件夹下所有文件的操作方法
怎样查看linux下的所有目录?~~~~~
BrightStor ARCserve Backup for Linux 可以对linux的指定目录进行备份吗
ASP遍历目录问题
linux 下 共享 ftp 目录
linux下怎么进入安装包的目录???
我Linux系统下的var里没有named目录
linux下用压缩目录的命令吗?
linux 下如何同时更改多级目录的属性
c盘下的一个文件目录?
LINUX下C程序的运行问题
linux下的100行c编程
linux 下 用什么命令将一个目录内的全部内容移到上级目录?
linux下如何实现输出翻页效果
跪求用C多重邻接表实现图的深度优先遍历和广度优先遍历
如何让MAKE输出的结果输出到指定文件夹,它默认放在当前文件夹下。
在没有条件指定数据库的前提下,如何输出第三条数据?
DirectX 9.0c解压文件到指定目录,这个指定目录到底是那????
求二叉树遍历算法C语言实现的
帮忙看看这个图的深度优先遍历程序.(C)
c输出的问题
linux下C编程问题????