#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
/*
函数功能: 遍历某一目录下的文件 及其子目录下的文件
程序编译运行环境:linux 下
函数的接口: 传入一个路径名
作者:ygt
*/
int browse_folder(char *pathname)
{
DIR *pdir;//定义一个目录流;就像是定义文件流一样
struct dirent *pentry;//定义目录结构体
char temp[256];
char path[256];
pdir = opendir(pathname);//打开一个路径并返回一个目录流。
if(pdir == NULL)
{
perror("opendir");
return 1;
}
while((pentry = readdir(pdir))!=NULL)
{
if(strcmp(pentry->d_name,".")==0 || strcmp(pentry->d_name,"..")==0)
continue;
if(pentry->d_type == DT_DIR)//判断是否是目录
{
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@FOLDER@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
printf("directory:%s\n",pentry->d_name);//输出文件夹名
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@FOLDER@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
strcpy(temp,pathname);
sprintf(path,"%s/%s",temp,pentry->d_name);
browse_folder(path);
sleep(1);
}else if(pentry->d_type == DT_BLK)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("block device file:\n",pentry->d_name);//输出块设备名
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}else if(pentry->d_type == DT_CHR)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("character device file:\n",pentry->d_name);//输出字符设备名
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
else if(pentry->d_type == DT_FIFO)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("named pipe file:\n",pentry->d_name);//
输出有名管道名
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}else if(pentry->d_type == DT_LNK)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("symbolic link file:\n",pentry->d_name);//输出符号链接名
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}else if(pentry->d_type == DT_REG)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
linux下的sleep函数printf("regular file:\n",pentry->d_name);//输出普通文件名
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}else if(pentry->d_type == DT_SOCK)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("Unix domain socket:\n",pentry->d_name);//输出套接字
printf("\t inode number: %d\n",pentry->d_ino);//输出i节点号
printf("\t offset to the next dirent: %d\n",pentry->d_off);//输出偏移位置
printf("\t length of this record: %d\n",pentry->d_reclen);//输出文件或文件夹的名字的长度
printf("\t type of file: %c\n",pentry->d_type);//输出文件或文件夹得类型号
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}else if(pentry->d_type == DT_UNKNOWN)
{
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("The file type is unknown\n");//未知的文件
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$FILE$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
}
}
return 0;
}
int main()
{
char pathname[256] = {"/home/yanggentao/3512mdvr/app_mdvr/uuu/"};
//search_file(pathname);
browse_folder(pathname);
return 0;
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论