您的位置:首页 > 其它

文件目录结构显示与文件搜索

2017-06-08 20:29 169 查看
#include <iostream>
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

#define MaxSize 20    //next字符串的长度

typedef struct{    //匹配串结构体
string data;
int length;
}SqString;

void getString(SqString &t,string s)    //初始化匹配串结构
{
int i;
t.length = s.length();
for(i=0; i<t.length; i++)
t.data[i] = s[i];
t.data[i] = '\0';
}

void GetNext(SqString &t, int next[])    //获取next
{
int j, k;
j = 0;
k = -1;
while(j < t.length-1)
{
if(k == -1 || t.data[j] == t.data[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
}

int KMPIndex(string s, SqString &t, int next[])    //匹配相应文件夹名
{
int i = 0;
int j = 0;
while(i<s.length() && j<t.length)
{
if(j ==-1 || s[i] == t.data[j])
{
i++;
j++;
}
else
j = next[j];
}
if(j >= t.length)
return (i-t.length);//匹配到了
else
return -1;//没有匹配到
}

void searchFile(string fileName,string filePath,SqString &t,int next[])    //文件查找
{
struct _finddata_t filefind;
string curr = filePath + "\\*.*";     //修改此处改变搜索条件
int done = 0, handle;
int index;

if((handle = _findfirst(curr.c_str(), &filefind)) != -1)
{
while(!(done = _findnext(handle, &filefind)))
{
if(strcmp(filefind.name, "..") == 0)
continue;
index = KMPIndex(filefind.name, t, next);
if((_A_SUBDIR != filefind.attrib))  // 不是目录
{
if(filefind.name == fileName || index > -1)
{
cout << filePath+"\\"+filefind.name << endl;
}
}
else
{
if(filefind.name == fileName || index > -1)
{
cout << filePath+"\\"+filefind.name << endl;
}
curr = filePath + "\\" + filefind.name;
searchFile(fileName, curr, t, next);   // 递归遍历子目录
}
}
_findclose(handle);
}
}

void fileIndex(string path, int layer)  //文件目录结构输出
{
struct _finddata_t filefind;
string curr = path + "\\*.*";  // 修改此处改变搜索条件
int done = 0, i, handle;
if((handle = _findfirst(curr.c_str(), &filefind)) != -1)
{
while(!(done = _findnext(handle, &filefind)))
{
if(strcmp(filefind.name, "..") == 0)
continue;
for(i=0; i<layer; i++)
{
cout << '\t';
}
if((_A_SUBDIR == filefind.attrib))  //是目录
{
cout<<"|_*"<<filefind.name<<endl;
curr = path + "\\" + filefind.name;
fileIndex(curr, ++layer);  // 递归遍历子目录
layer--;
}
else
{
cout<<"|_"<<filefind.name<<endl;
}
}
_findclose(handle);
}
}

int main()
{
int order=0;
string filePath;  //文件路径
string fileName;  //被搜索文件名
int next[MaxSize];
next[0] = -1;
SqString s;
while(true)
{
cout << "********************************************" << endl;
cout << "**   1.文件目录输出     2.文件搜索        **" << endl;
cout << "**   0.退出                               **" << endl;
cout << "********************************************" << endl;
cout << "请输入您的指令:";
while(1)
{
cin >> order;
if(order>=0 && order<=2)
break;
printf("输入指令有误,请重新输入您的指令:");
}
switch(order)
{
case 1:
system("cls");
cout << "请输入您要搜索的文件路径(全盘搜索则输入‘0’):";
cin >> filePath;
fileIndex(filePath, 0);////////添加写入文件  充分展示结构
break;
case 2:
system("cls");
cout << "请输入您要搜索的文件名:";
cin >> fileName;
cout << "请输入您要搜索的文件路径(全盘搜索则输入‘0’):";
cin >> filePath;
getString(s, fileName);
GetNext(s, next);
if(filePath == "0")
{
searchFile(fileName, "C:", s, next);
searchFile(fileName, "D:", s, next);
searchFile(fileName, "E:", s, next);
}
else
{
searchFile(fileName, filePath, s, next);
}
break;
case 0:
return 0;
default:
cout << "指令输入有误,请重新输入!!!";
}
cout << "回车键继续";
getchar();
getchar();
system("cls");
}

return 0;
}


注意:1.全盘搜索部分根据个人电脑设定盘符,不知道有没有办法自动读取全盘,大神指导可留言指教。

     2.文件的输出最好存在一个多叉树中,提高代码水平

     3.文件的读取速度太慢,不要读取文件过多的文件,演示的时候时间太长

心得体会:指令防错没搞明白,没有用树形结构存储文件降低了代码的档次,所以验收的时候分值很低。不要拖延,一个星期的活干了两个月。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: