一、目录IO
1)目录在系统里面,也是文件,只是文件的类型不一样
二、函数
基本接口:
opendir
readdir:获取读到目录项 的名字(字符串匹配)
1.opendir
enum//union
{
DT_UNKNOWN = 0, //未知
# define DT_UNKNOWN DT_UNKNOWN
DT_FIFO = 1, //管道文件(有名,无名)
# define DT_FIFO DT_FIFO
DT_CHR = 2, //字符设备(驱动)
# define DT_CHR DT_CHR
DT_DIR = 4, //目录文件
# define DT_DIR DT_DIR
DT_BLK = 6, //块设备(驱动)
# define DT_BLK DT_BLK
DT_REG = 8, //普通文件
# define DT_REG DT_REG
DT_LNK = 10, //链接文件(软链接,硬链接)
# define DT_LNK DT_LNK
DT_SOCK = 12,//套接字文件
# define DT_SOCK DT_SOCK
---------------------------------------------
DT_WHT = 14
# define DT_WHT DT_WHT
};
1.明白工作路径是指当前路径,如果查找文件不在当前工作路径的目录下也就意味着,我们需要使用绝对路径、
目录检索思路
①首先从main()函数中输入我们所需要检索的路径和文件名,输入格式如下
./my_find xxx
②在目录中不仅仅会存在文件,还可能会存在目录,所以我们检索遇到目录的时候因该优先进入目录直到当前路径下没有目录位置。
③之后在当前目录检索所有文件,然后返回上一级目录,检索上一级目录(存在其他目录的话依旧是优先进入目录中,然后检索)--这就可以通过递归来实现。
④在检索过程中我们还要能够实现不会重复检索。如何实现不重复检索?
⑤当我们找到了我们所需要的文件的时候,就需要跳出这个递归。我们如何保存入口到当前文件的路径?我们可以通过制造一个铲子,每次循环在判断前,铲子里都是空的,在判断的过程中将路径写入铲子里面。 使用sprintf实现很简单
⑦有一个很重要的就是,之前我们打印一个空目录时发现里面其实已经存在两个东西了了,分别是“.”“..”所以在判断前,我们需要先跳过这两个数据。
⑧还有一个“/”问题,就是有人在输入路径的时候会习惯在后面打印/所以判断一下
代码思路:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <dirent.h>
int find_file(char *path, char *file_name);
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("错误指令\n");
return -1;
}
find_file(argv[1], argv[2]);
return 0;
}
//实现函数封装
int find_file(char *obj_path, char *obj_file)
{
//当前路径层开始检索
DIR *dp = opendir(obj_path);
if (dp == NULL)
{
perror("opendir");
return -1;
}
struct dirent *eq = NULL;
char TQ[260];
while (1)
{
memset(TQ, 0, 260);
eq = readdir(dp);
if (eq == NULL)
{
break;
}
//忽略. ..
if (eq->d_name[0] == '.')
{
continue;
}
//拼接名字路径
if (obj_path[strlen(obj_path) - 1] == '/')
{
sprintf(TQ, "%s%s", obj_path, eq->d_name);
}
else
{
sprintf(TQ, "%s/%s", obj_path, eq->d_name);
}
//这里就可以判断是文件还是目录了
if (eq->d_type == DT_DIR)
{
find_file(TQ, obj_file);
//这里的传参??
} //目录为DT_DIR(4)这里就可以递归了
if (eq->d_type == DT_REG)
{
if (strcmp(eq->d_name, obj_file) == 0)
{
printf("%s\n", TQ);
}
} //文件为DT_REG(8)
}
closedir(dp);
return 0;
}
千万不要把closedir 放到whlie循环里///。。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <dirent.h>
int find_file(char *path, char *file_name);
int main(int argc, char *argv[])
{
if (argc != 3)
{
printf("错误指令\n");
return -1;
}
find_file(argv[1], argv[2]);
return 0;
}
//实现函数封装
int find_file(char *obj_path, char *obj_file)
{
//当前路径层开始检索
DIR *dp = opendir(obj_path);
if (dp == NULL)
{
perror("opendir");
return -1;
}
struct dirent *eq = NULL;
char TQ[260];
while (1)
{
memset(TQ, 0, 260);
eq = readdir(dp);
if (eq == NULL)
{
break;
}
//忽略. ..
if (eq->d_name[0] == '.')
{
continue;
}
//拼接名字路径
if (obj_path[strlen(obj_path) - 1] == '/')
{
sprintf(TQ,"%s%s", obj_path, eq->d_name);
}
else
{
sprintf(TQ,"%s/%s", obj_path, eq->d_name);
}
//这里就可以判断是文件还是目录了
if (eq->d_type == DT_DIR)
{
find_file(TQ,obj_file);
//这里的传参??
} //目录为DT_DIR(4)这里就可以递归了
if (eq->d_type == DT_REG)
{
if (strcmp(eq->d_name,obj_file) == 0)
{
printf("%s\n", TQ);
}
} //文件为DT_REG(8)
}
closedir(dp);
return 0;
}