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 */
};
三个函数介绍完了,直接来一个例子吧:
**********************************************SearchDir.c****************************************************
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
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;
}
*****************************************************************************************************************