操作系统课程设计文件管理_第1页
操作系统课程设计文件管理_第2页
操作系统课程设计文件管理_第3页
操作系统课程设计文件管理_第4页
操作系统课程设计文件管理_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、#include stdio.h #include string.h #include malloc.h #include stdlib.h #define max 1000 struct file/* 普通文件的结构体*/ /int type;/0无作用,当做一个空节点存在;1 为记录型文件;2 为执行文件/ 前两个变量为文件的权限设置,1 为允许操作, 0 为不允许操作int write;/ 可写int read;/ 可读int length;/ 文件的长度char chmax; ; typedef struct file file; typedef struct ffile/*定义文件类

2、型的结构体*/ int type;/1为文件夹;2 为文件;char name20;/ 文件 (夹)名字int open;/ 文件打开标志,0 为关, 1 为开file iffile;/ 如果为文件时有的信息struct ffile *parent;/指向上一级文件的指针struct ffile *brother;/指向同级兄弟文件(夹 )的指针struct ffile *child;/指向下一级文件(夹)的指针ffile; typedef ffile *ffile; /*typedef struct open/*记录打开文件的结构体 char name20;/ 记录打开文件(夹)的名字ffi

3、le* add;/ 记录打开文件上一级文件地址的指针open;*/ / 全局变量ffile user1;/ 用户 1 ffile user2;/ 用户 2 ffile copyf;/ 记录被复制文件(夹)的上一级文件地址/open openf20;/记录打开文件的队列ffile init(void)/* 初始化,创建根结点*/ ffile c; 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; st

4、rcpy(c-name,file1); c-parent=null; c-child=null; c-brother=null; strcpy(c-iffile.ch,null); return(c); /*void initopen() int a,b; a=20; for(b=1;bchild; if(user=null) printf( 该文件内没有任何文件(夹)。n); return; printf(n); for(;user!=null;) printf(name); if(user-type=2) /*if(user-iffile.type=1) printf(/ 记录型文件 /)

5、; else printf(/ 执行文件 /);*/ printf(/%dk,user-iffile.length); else printf(/ 文件夹 ); printf(n); user=user-brother; void creatf(ffile user)/* 创建文件| 文件夹 */ ffile parent; char ch20; /ffile user0; /parent=(ffile*)malloc(sizeof(ffile); parent=user; printf( 输入要创建文件(夹 )的名字: n); scanf(%s,ch); if(user-child=null

6、) 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-broth

7、er; / 设置新文件 (夹)的信息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 允许;

8、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); void deletechildtree(ffile user)/*删除子树 -结合 deletefile(); 使用 */ if (user-b

9、rother!=null)/ 从下到上,从右到左删除 deletechildtree(user-brother); if (user-child!=null) deletechildtree(user-child); if(user!=null) free(user); void deletefile(ffile user,char ch20)/*删除文件| 文件夹 */ ffile p,parent; int a; parent=user; if(user-child=null) printf( 错误:删除失败,该目录下没有可删除的文件(夹)!n); return; user=user-ch

10、ild; p=user; for(a=1;a+)/ 找出要删除文件的所在位置 if(!strcmp(user-name,ch) break; if(user-brother=null) printf( 错误:删除失败,当前位置没有该文件!n); return; if(a1) p=user; user=user-brother; if(user-open=1)/ 判断文件的开关情况 printf( 错误:删除失败,选择文件处于打开状态!n); return; if(p=user)/ 被删文件在文件队列的开头 if(user-brother=null)/ 该文件队列只有有一个文件 parent-c

11、hild=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-br

12、other=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); ffile openfolder(ffile user)/* 打开文件夹 */ /int a,b; /a=0;

13、 /*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;bopen=1;/ 设置文件为打开/strcpy(,user-name); /openfb.add=user; printf( 文件夹打开成功。n); return(user);/ 返回被打开的文件夹的地址 void openfile(ffile

14、user)/* 打开普通文件*/ if(user-open=1) printf( 错误:打开失败,此文件已经被打开!n); return; user-open=1; printf( 普通文件打开成功!n); ffile openff(ffile user)/* 打开文件 (夹)*/ char ch20; ffile parent; int a; printf( 选择要打开的文件名:n); scanf(%s,ch); parent=user; if(user-child=null) printf( 错误:打开失败,该目录下没有可打开的文件(夹)!n); return(parent); user=

15、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); else if(user-type=2) printf( 开始打开普通文件。 。 。n); openfile(user); user=user-parent;

16、 return(user); void closefile(ffile user)/* 关闭普通文件 */ char ch20; int a; 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); retu

17、rn; user=user-brother; if(user-open=0) printf( 错误:关闭失败,该文件已经是关闭状态!n); return; user-open=0; printf( 文件已经成功关闭!); /* 没有文件夹关闭原因:文件夹一打开就会跳向打开的新文件里而文件夹关闭就会直接返回上一级的目录,若想整个文件夹都关闭,直接退出就可以了因此不会直接关闭某个特定的文件*/ ffile backf(ffile user)/*返回上一层目录*/ if(user-parent=null) printf( 错误:返回失败,此处是最顶层目录!n); return(user); user

18、-open=0; user=user-parent; return(user); void readfile(ffile user)/* 读文件 */ char ch20; int a; 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( 错误:读

19、取失败,当前位置没有该文件!n); return; user=user-brother; if(user-open=0) printf( 错误:文件读取失败,该文件处于关闭状态!n); return; else if(user-iffile.read=0) printf( 错误:文件读取失败,该文件受保护,禁止读取!n); return; printf( 读操作,该文件中的内容:n); if(!strcmp(user-iffile.ch,null) printf( 该文件内没有可读内容!n); return; printf(%sn,user-iffile.ch); printf( 文件读取成功

20、!n); void writefile(ffile user)/* 写文件 */ char ch20; int a; 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=

21、user-brother; if(user-open=0) printf( 错误:文件写操作失败,该文件处于关闭状态!n); return; else if(user-iffile.write=0) printf( 错误:文件写操作失败,该文件受保护,禁止写!n); return; printf( 写操作 ,输入内容: n); scanf(%s,user-iffile.ch); user-iffile.length=strlen(user-iffile.ch); printf( 文件进行写操作成功!n); ffile copyfile(ffile user,ffile copyf)/* 拷贝文

22、件 */ char ch20; int a; 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;

23、copyf=user; printf( 拷贝成功! n); return(copyf); ffile fenpei(ffile copyf,ffile user,ffile parent)/* 粘贴时,给已拷贝项分配内存空间,以及给对应信息赋值 */ user=(ffile*)malloc(sizeof(ffile); /parent对 child 的连接,以及brother 之间的连接已经完成if(copyf-brother=null & copyf-child=null) user-parent=parent; user-child=null; user-brother=null;

24、 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

25、连接,自己孩子的父结点就是自己 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-

26、iffile.write; strcpy(user-iffile.ch,copyf-iffile.ch); return(user); void prastefile(ffile user,ffile copyf)/* 粘贴文件 */ /user 是要粘贴的地方,copyf 是要粘贴的内容,/ 有相同文件名的会判断会不会覆盖,或者是重命名/ 在原树中进行新建操作 int i,j; char ch20; ffile p,user0,parent; parent=user;/ 记录父结点user=user-child; p=user;/ 记录当前结点的前一个brother 结点strcpy(ch,

27、null); if(copyf=null)/ 判断有没有拷贝文件 printf( 错误:粘贴失败,还没有拷贝任何文件(夹)!n); return; /p=(ffile*)malloc(sizeof(ffile); /p-child=(ffile*)malloc(sizeof(ffile); / 先给粘贴项分配内存空间/p-child=fenpei(copyf,p-child,p); if(user=null) / 当前位置没有任何文件结点 user=fenpei(copyf,user,parent);/ 是他自己要分配,不是孩子结点! parent-child=user; user-broth

28、er=null; user-parent=parent; return; / 该位置没有任何文件for(j=1;j+) if(user-type=0) / 开端的空结点,用新结点覆盖,即:当前位置没有文件结点 user=user-parent; deletechildtree(p); user=fenpei(copyf,user-child,user);/返还增加的结点user-brother=null; user-parent=parent; parent-child=user; return; if(!strcmp(user-name,copyf-name) printf( 提示:该文件名

29、已经存在!n); printf( 请重命名文件:n); printf( 输入新文件名:n); scanf(%s,ch); if(user-brother=null) / 普通的退出条件 break; p=user; user=user-brother; user-brother=fenpei(copyf,user-brother,user-parent); user-brother-parent=user-parent; / 若要更名,粘贴分配完内存空间返回时再改变if(strcmp(ch,null) strcpy(user-brother-name,ch); printf( 粘贴成功。 n)

30、; void showroute(ffile user)/* 显示当前路径*/ if(user-parent!=null) showroute(user-parent); printf(%s/,user-name);/路径中每个结点的输出项 void change(ffile user) char ch20; int a,b; if(user-child=null) printf( 错误:属性修改失败,该目录下没有可修改的文件!n); return; printf( 选择要进行属性修改的文件(夹)名:n); scanf(%s,ch); user=user-child; for(a=1;a+)/

31、 找出要拷贝文件的所在位置,用user 替代 if(!strcmp(user-name,ch) break; if(user-brother=null) printf( 错误:属性修改失败,当前位置没有该文件!n); return; user=user-brother; if(user-type=1) printf( 错误:文件夹不能进行属性修改!n); return; for(;) printf(1. 修改读权限;n); printf(2. 修改写权限;n); printf(3. 返回; n); printf( 选择操作: n); scanf(%d,&a); if(a=1) prin

32、tf(0. 禁止;1.允许; n); printf( 请选择: n); scanf(%d,&b); user-iffile.read=b; printf( 修改成功! n); else if(a=2) printf(0. 禁止;1.允许; n); printf( 请选择: n); scanf(%d,&b); user-iffile.write=b; printf( 修改成功! n); else if(a=3) return; else printf( 错误:没有该操作!n); void main()/* 主函数 */ ffile d,e,f;/f 记录当前显示界面父结点位置int a,b,c; char ch20; a=0; printf(*目录 *n); printf( 1.选择用户 n); printf( 2.退出 n); printf(*n); for(;) printf( 选择操作: n); scanf(%d,&a); if (a=1) printf( 选择用户: n); printf(1.user1;n2.user2;n); scanf(%d,&b); break; else if (a=2) printf( 欢迎使用。 n); exit(0);

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论