操作系统课程设计文件管理.docx
《操作系统课程设计文件管理.docx》由会员分享,可在线阅读,更多相关《操作系统课程设计文件管理.docx(32页珍藏版)》请在冰点文库上搜索。
操作系统课程设计文件管理
#include"stdio.h"
#include"string.h"
#include"malloc.h"
#include"stdlib.h"
#defineMAX1000
structfile/*普通文件的结构体*/
{
//inttype;//0无作用,当做一个空节点存在;1为记录型文件;2为执行文件
//前两个变量为文件的权限设置,1为允许操作,0为不允许操作
intwrite;//可写
intread;//可读
intlength;//文件的长度
charch[MAX];
};
typedefstructfileFile;
typedefstructffile/*定义文件类型的结构体*/
{
inttype;//1为文件夹;2为文件;
charname[20];//文件(夹)名字
intopen;//文件打开标志,0为关,1为开
Fileiffile;//如果为文件时有的信息
structffile*parent;//指向上一级文件的指针
structffile*brother;//指向同级兄弟文件(夹)的指针
structffile*child;//指向下一级文件(夹)的指针
}Ffile;
typedefFfile*FFile;
/*typedefstructOpen/*记录打开文件的结构体
{
charname[20];//记录打开文件(夹)的名字
FFile*add;//记录打开文件上一级文件地址的指针
}Open;*/
//全局变量
FFileuser1;//用户1
FFileuser2;//用户2
FFilecopyf;//记录被复制文件(夹)的上一级文件地址
//Openopenf[20];//记录打开文件的队列
FFileinit(void)/*初始化,创建根结点*/
{
FFilec;
c=(Ffile*)malloc(sizeof(Ffile));
c->type=2;
c->open=0;
//c->iffile.type=2;
c->iffile.write=1;
c->iffile.read=1;
c->iffile.length=0;
strcpy(c->name,"file1");
c->parent=NULL;
c->child=NULL;
c->brother=NULL;
strcpy(c->iffile.ch,"NULL");
return(c);
}
/*voidinitopen()
{
inta,b;
a=20;
for(b=1;b<=a;b++)
{
openf[b].add=NULL;
}
}*/
//传递要显示文件的parent的地址
voidshow(FFileuser)/*显示当前界面存在的文件*/
{
user=user->child;
if(user==NULL)
{
printf("该文件内没有任何文件(夹)。
\n");
return;
}
printf("\n");
for(;user!
=NULL;){
printf("<%s",user->name);
if(user->type==2)
{
/*if(user->iffile.type==1)
printf("/记录型文件/");
else
printf("/执行文件/");*/
printf("/%dk",user->iffile.length);
}
else
{
printf("/文件夹");
}
printf(">\n");
user=user->brother;
}
}
voidcreatf(FFileuser)/*创建文件||文件夹*/
{
FFileparent;
charch[20];
//FFileuser0;
//parent=(Ffile*)malloc(sizeof(Ffile));
parent=user;
printf("输入要创建文件(夹)的名字:
\n");
scanf("%s",ch);
if(user->child==NULL)
{
user->child=(Ffile*)malloc(sizeof(Ffile));
user=user->child;
}else
{
user=user->child;
for(;;)
{
if(user->type==0)//开端的空结点,用新结点覆盖
break;
if(!
strcmp(user->name,ch))
{
printf("错误:
该文件名已经存在,文件(夹)创建失败!
\n");
return;
}
if(user->brother==NULL)
{
user->brother=(Ffile*)malloc(sizeof(Ffile));
user=user->brother;
break;
}
user=user->brother;
}
}
//设置新文件(夹)的信息
strcpy(user->name,ch);
printf("选择创建对象:
1文件夹;2文件;\n");
scanf("%d",&user->type);
user->open=0;
if(user->type==2)//添加文件信息
{
//printf("选择文件类型:
1记录型文件;2执行文件;\n");
//scanf("%d",&user->iffile.type);
printf("能否对文件进行读:
0禁止;1允许;\n");
scanf("%d",&user->iffile.read);
printf("能否对文件进行写:
0禁止;1允许;\n");
scanf("%d",&user->iffile.write);
//printf("设置文件大小(单位:
K):
\n");
//scanf("%d",&user->iffile.length);
user->iffile.length=0;
strcpy(user->iffile.ch,"NULL");
}
user->brother=NULL;
user->child=NULL;
user->parent=parent;
printf("文件创建成功!
\n");
}
voiddeletechildtree(FFileuser)/*删除子树--结合deletefile();使用*/
{
if(user->brother!
=NULL)//从下到上,从右到左删除
{
deletechildtree(user->brother);
}
if(user->child!
=NULL)
{
deletechildtree(user->child);
}
if(user!
=NULL)
{
free(user);
}
}
voiddeletefile(FFileuser,charch[20])/*删除文件||文件夹*/
{
FFilep,parent;
inta;
parent=user;
if(user->child==NULL)
{
printf("错误:
删除失败,该目录下没有可删除的文件(夹)!
\n");
return;
}
user=user->child;
p=user;
for(a=1;;a++)//找出要删除文件的所在位置
{
if(!
strcmp(user->name,ch))
break;
if(user->brother==NULL)
{
printf("错误:
删除失败,当前位置没有该文件!
\n");
return;
}
if(a>1)
p=user;
user=user->brother;
}
if(user->open==1)//判断文件的开关情况
{
printf("错误:
删除失败,选择文件处于打开状态!
\n");
return;
}
if(p==user)//被删文件在文件队列的开头
{
if(user->brother==NULL)//该文件队列只有有一个文件
{
parent->child=NULL;
if(user->child!
=NULL)//删除的是文件(夹)子树
{
deletechildtree(user);
}else
{
free(user);//删除的是文件(夹)结点
}
printf("删除成功!
\n");
return;
}
//文件队列有多个文件
p=user->brother;
parent->child=p;
p->parent=parent;
if(user->child!
=NULL)
{
deletechildtree(user);
}else
{
free(user);
}
printf("删除成功!
\n");
return;
}
else//被删文件不在队列开头
{
if(user->brother==NULL)//被删文件在文件队列最末尾
{
p->brother=NULL;
if(user->child!
=NULL)
{
deletechildtree(user);
}else
{
free(user);
}
printf("删除成功!
\n");
return;
}
//被删文件在文件队列中间
p->brother=user->brother;
if(user->child!
=NULL)
{
deletechildtree(user);
}
else
{
free(user);
}
}
printf("删除成功!
\n");
}
FFileopenfolder(FFileuser)/*打开文件夹*/
{
//inta,b;
//a=0;
/*if(user->child==NULL)
{
user->child=(Ffile*)malloc(sizeof(Ffile));
user->child->type=0;
user->child->brother=NULL;
user->child->parent=user;
user->child->child=NULL;
}
/*for(b=1;b<=20;b++)
{
if(openf[b].add!
=NULL)
a++;
}
if(a==20)
{
printf("错误:
打开列表溢出!
");
return(user);
}
for(b=1;;b++)
{
if(openf[b].add==NULL)
break;
}*/
user->open=1;//设置文件为打开
//strcpy(openf[b].name,user->name);
//openf[b].add=user;
printf("文件夹打开成功。
\n");
return(user);//返回被打开的文件夹的地址
}
voidopenfile(FFileuser)/*打开普通文件*/
{
if(user->open==1)
{
printf("错误:
打开失败,此文件已经被打开!
\n");
return;
}
user->open=1;
printf("普通文件打开成功!
\n");
}
FFileopenff(FFileuser)/*打开文件(夹)*/
{
charch[20];
FFileparent;
inta;
printf("选择要打开的文件名:
\n");
scanf("%s",ch);
parent=user;
if(user->child==NULL)
{
printf("错误:
打开失败,该目录下没有可打开的文件(夹)!
\n");
return(parent);
}
user=user->child;
for(a=1;;a++)//找出要打开文件的所在位置
{
if(!
strcmp(user->name,ch))
break;
if(user->brother==NULL)
{
printf("错误:
打开失败,当前位置没有该文件!
\n");
return(parent);
}
user=user->brother;
}
if(user->type==1)
{
printf("开始打开文件夹。
。
。
\n");
user=openfolder(user);
}
elseif(user->type==2)
{
printf("开始打开普通文件。
。
。
\n");
openfile(user);
user=user->parent;
}
return(user);
}
voidclosefile(FFileuser)/*关闭普通文件*/
{
charch[20];
inta;
printf("选择要打开的文件名:
\n");
scanf("%s",ch);
if(user->child==NULL)
{
printf("错误:
关闭失败,该目录下没有可关闭的文件!
\n");
return;
}
user=user->child;
for(a=1;;a++)//找出要关闭文件的所在位置
{
if(!
strcmp(user->name,ch))
break;
if(user->brother==NULL)
{
printf("错误:
关闭失败,当前位置没有该文件!
\n");
return;
}
user=user->brother;
}
if(user->open==0)
{
printf("错误:
关闭失败,该文件已经是关闭状态!
\n");
return;
}
user->open=0;
printf("文件已经成功关闭!
");
}
/*没有文件夹关闭原因:
文件夹一打开就会跳向打开的新文件里
而文件夹关闭就会直接返回上一级的目录,
若想整个文件夹都关闭,直接退出就可以了
因此不会直接关闭某个特定的文件*/
FFilebackf(FFileuser)/*返回上一层目录*/
{
if(user->parent==NULL)
{
printf("错误:
返回失败,此处是最顶层目录!
\n");
return(user);
}
user->open=0;
user=user->parent;
return(user);
}
voidreadfile(FFileuser)/*读文件*/
{
charch[20];
inta;
printf("选择要读取的文件名:
\n");
scanf("%s",ch);
if(user->child==NULL)
{
printf("错误:
读取失败,该目录下没有可读取的文件!
\n");
return;
}
user=user->child;
for(a=1;;a++)//找出要读取文件的所在位置
{
if(!
strcmp(user->name,ch))
break;
if(user->brother==NULL)
{
printf("错误:
读取失败,当前位置没有该文件!
\n");
return;
}
user=user->brother;
}
if(user->open==0)
{
printf("错误:
文件读取失败,该文件处于关闭状态!
\n");
return;
}
elseif(user->iffile.read==0)
{
printf("错误:
文件读取失败,该文件受保护,禁止读取!
\n");
return;
}
printf("读操作,该文件中的内容:
\n");
if(!
strcmp(user->iffile.ch,"NULL"))
{
printf("该文件内没有可读内容!
\n");
return;
}
printf("%s\n",user->iffile.ch);
printf("文件读取成功!
\n");
}
voidwritefile(FFileuser)/*写文件*/
{
charch[20];
inta;
printf("选择要进行写操作的文件名:
\n");
scanf("%s",ch);
if(user->child==NULL)
{
printf("错误:
写操作失败,该目录下没有可写的文件!
\n");
return;
}
user=user->child;
for(a=1;;a++)//找出要读取文件的所在位置
{
if(!
strcmp(user->name,ch))
break;
if(user->brother==NULL)
{
printf("错误:
写操作失败,当前位置没有该文件!
\n");
return;
}
user=user->brother;
}
if(user->open==0)
{
printf("错误:
文件写操作失败,该文件处于关闭状态!
\n");
return;
}
elseif(user->iffile.write==0)
{
printf("错误:
文件写操作失败,该文件受保护,禁止写!
\n");
return;
}
printf("写操作,输入内容:
\n");
scanf("%s",user->iffile.ch);
user->iffile.length=strlen(user->iffile.ch);
printf("文件进行写操作成功!
\n");
}
FFilecopyfile(FFileuser,FFilecopyf)/*拷贝文件*/
{
charch[20];
inta;
printf("选择要进行拷贝的文件(夹)名:
\n");
scanf("%s",ch);
if(user->child==NULL)
{
printf("错误:
拷贝失败,该目录下没有可拷贝的文件!
\n");
return(NULL);
}
user=user->child;
for(a=1;;a++)//找出要拷贝文件的所在位置,用user替代
{
if(!
strcmp(user->name,ch))
break;
if(user->brother==NULL)
{
printf("错误:
拷贝失败,当前位置没有该文件!
\n");
return(NULL);
}
user=user->brother;
}
copyf=user;
printf("拷贝成功!
\n");
return(copyf);
}
FFilefenpei(FFilecopyf,FFileuser,FFileparent)/*粘贴时,给已拷贝项分配内存空间,以及给对应信息赋值*/
{
user=(Ffile*)malloc(sizeof(Ffile));
//parent对child的连接,以及brother之间的连接已经完成
if(copyf->brother==NULL&©f->child==NULL)
{
user->parent=parent;
user->child=NULL;
user->brother=NULL;
}
else{
if(copyf->brother!
=NULL)
{
user->brother=fenpei(copyf->brother,user->brother,parent);
//brother连接,兄弟节点有同一个父结点
user->brother->parent=user->parent;
}
else
{
user->brother=NULL;
}
if(copyf->child!
=NULL)
{
//parent=p;
user->child=fenpei(copyf->child,user->child,user);
user->child->parent=user;//完成child对parent的连接
//child连接,自己孩子的父结点就是自己
}
else
{
user->child=NULL;
user->child->parent=user;
}
}
//设置结点对应的信息
strcpy(user->name,copyf->name);
user->open=copyf->open;
user->type=copyf->type;
if(user->type==2)
{
user->iffile.length=copyf->iffile.length;
user->iffile.read=copyf->iffile.read;
//user->iffile.type=copyf->iffile.type;
user->iffile.write=copyf->iffile.write;
strcpy(user->iffile.ch,copyf->iffile.ch);
}
return(user);
}
voidprastefile(FFileuser,FFilecopyf)/*粘贴文件*/
//user是要粘贴的地方,copyf是要粘贴的内容,
//有相同文件名的会判断会不会覆盖,或者是重命名
//在原树中进行新建操作
{
inti,j;
charch[20];
FFilep,user0,parent;
parent=us