文件管理.docx
《文件管理.docx》由会员分享,可在线阅读,更多相关《文件管理.docx(26页珍藏版)》请在冰豆网上搜索。
文件管理
华中师范大学计算机科学系
《操作系统》
实验报告
实验题目:
文件管理
学生姓名:
学生学号:
日期:
2011-12-18
实验4文件管理
【实验目的】
1)了解磁盘的物理组织。
2)掌握windows系统提供的有关对磁盘操作的函数。
3)根据输入的扇区号读/写指定扇区。
【实验准备知识】
需要了解已经学过的VC++工程文件的运行步骤以及它的操作界面常用的设置方法。
【实验内容】
在已做过的实验五、六的基础上编写一个程序,分别完成如下功能。
1)有个help帮助提示。
2)查看位示图。
3)在目录文件中创建子目录。
4)根据当前目录切换到指定目录。
5)搜索所要删除的目录是否为空目录,若是则删除。
6)在当前目录中创建文件名称。
7)搜索所要删除的文件是否存在,若是则删除。
8)列出当前目录的所有目录项,退出程序等。
实验要求:
深入理解操作系统设备当作文件处理的特性。
参考源代码:
//wjgl.cpp:
implementationofthewjglclass.
//
//////////////////////////////////////////////////////////////////////
#include"wjgl.h"
//////////////////////////////////////////////////////////////////////
//Construction/Destruction
//////////////////////////////////////////////////////////////////////
wjgl:
:
wjgl()
{
hang=16;
lie=16;
intj,k;
//cout<<"矩阵的行"<//cin>>hang;
//cout<<"矩阵的列"<//cin>>lie;
srand(time(NULL));
for(j=0;j{
juzhen[0][j]=1;
}
for(j=1;j{
for(k=0;kjuzhen[j][k]=(rand()%2);
}
cout<<"当前没有任何目录和文件,系统为你自动进入C:
,如需帮助请选择H"<ml=(mulu*)malloc(sizeof(mulu));
zhizhen=(mulu*)malloc(sizeof(mulu));
zhizhen=ml;
strcpy(ml->name,"C");
ml->awenjian=NULL;
ml->top=NULL;
ml->next=NULL;
ml->amulu=NULL;
}
wjgl:
:
~wjgl()
{
}
intwjgl:
:
cd()
{
node1*s;
node1*r;
r=(node1*)malloc(sizeof(node1));
s=(node1*)malloc(sizeof(node1));
s=ml;
r=zhizhen;
charmlname[20];
cin>>mlname;
if(!
strcmp(mlname,".."))
{
if(zhizhen->top==NULL)
{
cout<<"不存在该目录的根目录"<return0;
}
else
{
zhizhen=zhizhen->top;
}
//showweizhi();
return0;
}
else
{
r=r->amulu;
while(r!
=NULL)
{
if(!
strcmp(r->name,mlname))
{
zhizhen=r;
//showweizhi();
return0;
}
else
{
if(r->next==NULL)
break;
r=r->next;
}
}
}
cout<<"目录不存在"<return0;
}
intwjgl:
:
mk()
{
structtm*newtime;
char*tmpbuf=newchar[120];
time_tlt;
time(<);
newtime=localtime(<);
strftime(tmpbuf,128,"%c",newtime);
//showweizhi();
node*s;
s=(node*)malloc(sizeof(node));
s=zhizhen->awenjian;
node*s1;
s1=(node*)malloc(sizeof(node));
//cout<<"请输入文件的名称"<charfname[20];
cin>>fname;
//cout<<"请输入文件的大小"<intdaxiao;
cin>>daxiao;
strcpy(s1->name,fname);
s1->daxiao=daxiao;
s1->next=NULL;
if(s==NULL)
{
zhizhen->awenjian=s1;
s1->time=tmpbuf;
//cout<<"文件创建成功"<//return0;
}
elseif(!
strcmp(s->name,fname))
{
cout<<"文件名字重复"<return0;
}
else
{
while(s->next!
=NULL)
{
if(!
strcmp(s->next->name,fname))
{
cout<<"文件名字重复"<return0;
}
s=s->next;
}
s->next=s1;
}
inti,j,k;
k=0;
inti1=-1;
intj1=-1;
for(i=0;i{
for(j=0;j{
if(juzhen[i][j]==0)
{
if(j1==-1)
{
s1->begin=i*lie+j+1;
if(daxiao==1)
{
juzhen[i][j]=-1;
s1->time=tmpbuf;
return0;
}
else
{
i1=i;
j1=j;
k++;
continue;
}
}
else
{
if(daxiao-k==1)
{
juzhen[i1][j1]=i*lie+j+1;
juzhen[i][j]=-1;
s1->time=tmpbuf;
return0;
}
juzhen[i1][j1]=i*lie+j+1;
i1=i;
j1=j;
k++;
}
}
}
}
s1->next=NULL;
s1->time=tmpbuf;
return0;
}
voidwjgl:
:
showweizhi()
{
cout.setf(3);
chardizhi[20][10];
inti=0;
intj;
node1*s;
node1*r;
r=(node1*)malloc(sizeof(node1));
s=(node1*)malloc(sizeof(node1));
r=zhizhen;
s=r->top;
strcpy(dizhi[i],r->name);
i++;
//cout<name<<"->";
while(s!
=NULL)
{
strcpy(dizhi[i],s->name);
//cout<name<<"->";
s=s->top;
i++;
}
for(j=i;j>0;j--)
{
cout<if(i==j)
cout<<":
\\";
else
cout<<"\\";
}
cout<<">";
}
intwjgl:
:
rd()
{
showweizhi();
node1*r;
r=(node1*)malloc(sizeof(node1));
r=zhizhen->amulu;
cout<<"输入要删除的目录的名字"<charming[20];
cin>>ming;
if(r==NULL)
{
cout<<"目录不存在"<return0;
}
if(!
strcmp(r->name,ming))
{
if(r->amulu!
=NULL||r->awenjian!
=NULL)
{
cout<<"目录不空不能删除"<return0;
}
zhizhen->amulu=r->next;
//cout<<"删除成功"<return0;
}
while(r->next!
=NULL)
{
if(!
strcmp(r->next->name,ming))
{
if(r->next->amulu!
=NULL||r->next->awenjian!
=NULL)
{
cout<<"目录不空不能删除"<return0;
}
r->next=r->next->next;
//cout<<"删除成功"<return0;
}
}
cout<<"目录不存在"<return0;
}
intwjgl:
:
md()
{
structtm*newtime;
char*tmpbuf=newchar[120];
time_tlt;
time(<);
newtime=localtime(<);
strftime(tmpbuf,128,"%c",newtime);
//showweizhi();
node1*r;
r=(node1*)malloc(sizeof(node1));
r=zhizhen->amulu;
charming[20];
cin>>ming;
if(r==NULL)
{
node1*s;
s=(node1*)malloc(sizeof(node1));
s->time=tmpbuf;
strcpy(s->name,ming);
s->awenjian=NULL;
s->amulu=NULL;
s->next=NULL;
s->top=zhizhen;
zhizhen->amulu=s;
}
elseif(!
strcmp(r->name,ming))
{
cout<<"目录已经存在"<return0;
}
else
{
while(r->next!
=NULL)
{
if(!
strcmp(r->next->name,ming))
{
cout<<"目录已经存在"<return0;
}
r=r->next;
}
node1*s;
s=(node1*)malloc(sizeof(node1));
s->time=tmpbuf;
strcpy(s->name,ming);
s->awenjian=NULL;
s->amulu=NULL;
s->next=NULL;
s->top=zhizhen;
r->next=s;
}
//cout<<"创建成功"<return0;
}
intwjgl:
:
del()
{
showweizhi();
node*s;
s=(node*)malloc(sizeof(node));
s=zhizhen->awenjian;
//cout<<"请输入文件的名称"<charming[20];
cin>>ming;
inti,j;
inttemp;
if(s==NULL)
{
cout<<"删除不成功"<return0;
}
if(!
strcmp(s->name,ming))
{
i=(s->begin-1)/lie;
j=(s->begin-1)%lie;
while(juzhen[i][j]!
=-1)
{
temp=juzhen[i][j];
juzhen[i][j]=0;
i=(temp-1)/lie;
j=(temp-1)%lie;
}
juzhen[i][j]=0;
zhizhen->awenjian=s->next;
cout<<"删除成功"<return0;
}
while(s->next!
=NULL)
{
if(!
strcmp(s->next->name,ming))
{
i=(s->begin-1)/lie;
j=(s->begin-1)%lie;
while(juzhen[i][j]!
=-1)
{
temp=juzhen[i][j];
juzhen[i][j]=0;
i=(temp-1)/lie;
j=(temp-1)%lie;
}
juzhen[i][j]=0;
s->next=s->next->next;
//cout<<"删除成功"<return0;
}
}
cout<<"文件不存在不能删除"<return0;
}
voidwjgl:
:
tree()
{
charc=getchar();
if(c=='')
jiansuo();
else
shuchu(zhizhen);
}
voidwjgl:
:
showweishitu()
{
//cout.width();
intj,k;
cout<<"位"<<"示图为"<for(j=0;j{
for(k=0;kcout<:
left)<cout<}
}
voidwjgl:
:
shuchu(mulu*sc)
{
inti,j,k;
i=0;
j=0;
k=0;
mulu*r1;
r1=(node1*)malloc(sizeof(node1));
r1=zhizhen;
mulu*r;
r=(node1*)malloc(sizeof(node1));
r=sc->amulu;
cout<<"
"<<".";
showweizhi();
cout<zhizhen=zhizhen->top;
if(zhizhen==NULL)
;
else
{cout<<"
"<<"..";
showweizhi();
cout<zhizhen=r1;
while(r!
=NULL)
{
i++;
cout<time<<""<name<<""<<""<r=r->next;
}
node*s;
s=(node*)malloc(sizeof(node));
s=sc->awenjian;
while(s!
=NULL)
{
j++;
k=k+s->daxiao;
cout<time<<""<name<<"大小"<<""<daxiao<<""<<""<s=s->next;
}
cout<<"当前共有目录"<
cout<<"当前共有文件"<"<intcount=0;
for(j=0;j{
for(k=0;kif(juzhen[j][k]==0)
count++;
}
cout<<"剩余"<}
voidwjgl:
:
jiansuo()
{
intk=0;
inti=0;
intj=0;
charname[20];
//cout<<"请输入名称"<cin>>name;
inttemp1=strlen(name);
mulu*r;
r=(node1*)malloc(sizeof(node1));
r=zhizhen->amulu;
node*s;
s=(node*)malloc(sizeof(node));
s=zhizhen->awenjian;
while(r!
=NULL)
{
if(panduan(name,r->name)==1)
{
i++;
cout<time<<""<name<<""<<"";
showweizhi();
cout<}
r=r->next;
}
while(s!
=NULL)
{
if(panduan(name,s->name)==1)
{
j++;
cout<time<<""<name<<"大小"<daxiao<<""<<"";
k=k+s->daxiao;
showweizhi();
cout<}
s=s->next;
}
cout<<"目录"<
cout<<"文件"<intcount=0;
for(j=0;j{
for(k=0;kif(juzhen[j][k]==0)
count++;
}
cout<<"剩余"<}
intwjgl:
:
panduan(chara[],charb[])
{
intbiaoji=0;
if(!
strcmp(a,b))
return1;
inttemp=strlen(a);
inttemp1=strlen(b);
inti,j;
for(i=0,j=0;i{
if(a[i]=='*')
{
if(i==temp-1)
return1;
biaoji=1;
j--;
continue;
}
if(biaoji==1)
{
while(a[i]!
=b[j])
{
j++;
if(i==temp||j==temp1-1)
break;
}
}
if(a[i]=='?
')
{
if(i==temp-1)
{
if(j==temp1-1)
return1;
else
return0;
}
}
elseif(a[i]==b[j])
continue;
else
return0;
}
if(i==temp&&j==temp1)
return1;
else
return0;
/*if(temp>temp1)
return0;
return1;*/
}
#include
#include"wjgl.h"
usingnamespacestd;
voidmenu()
{
cout<<"H-----------------help"<cout<<"S----------------查看位示图"<cout<<"MD----------------在目录文件中创建子目录"<cout<<"CD----------------根据当前目录切换到指定目录"<cout<<"RD----------------搜索所要删除的目录是否为空目录,若是则删除"<cout<<"MK----------------在当前目录中创建文件名称"<cout<<"DEL---------------搜索所要删除的文件是否存在,若是则删除"<cout<<"DIR---------------列出当前目录的所有目录项"<cout<<"E-----------------退出程序"<}
intmain()
{
wjglawjgl;
//menu();
while
(1)
{
awjgl.showweizhi();
//cout<<"请选择操作"<charxuanxiang[20];
cin>>xuanxiang;
if(!
strcmp(xuanxiang,"H")||!
strcmp(xuanxiang,"h"))
{
menu();
}
if(!
strcmp(xuanxiang,"MD")||!
strcmp(xuanxiang,"md"))
{
awjgl.md()