2022年操作系统课程设计文件管理 2

上传人:夏** 文档编号:567338657 上传时间:2024-07-20 格式:PDF 页数:20 大小:114.29KB
返回 下载 相关 举报
2022年操作系统课程设计文件管理 2_第1页
第1页 / 共20页
2022年操作系统课程设计文件管理 2_第2页
第2页 / 共20页
2022年操作系统课程设计文件管理 2_第3页
第3页 / 共20页
2022年操作系统课程设计文件管理 2_第4页
第4页 / 共20页
2022年操作系统课程设计文件管理 2_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《2022年操作系统课程设计文件管理 2》由会员分享,可在线阅读,更多相关《2022年操作系统课程设计文件管理 2(20页珍藏版)》请在金锄头文库上搜索。

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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 20 页 - - - - - - - - - c=(Ffile*)malloc(

4、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); /*void initopen() int a,b; a=20; for(b=1;bchild; if(user=NULL) printf( 该文件内没有任何文件(夹)。n);

5、return; printf(n); for(;user!=NULL;) printf(name); if(user-type=2) /*if(user-iffile.type=1) printf(/ 记录型文件 /); else printf(/ 执行文件 /);*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 20 页 - - - - - - - - - printf(/%dk,user-iffile.length); else printf(/ 文件夹 ); p

6、rintf(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) user-child=(Ffile*)malloc(sizeof(Ffile); user=user-child; else user=user-child;

7、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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - -

8、第 3 页,共 20 页 - - - - - - - - - / 设置新文件 (夹)的信息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( 能否对文件进

9、行写: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); void deletechildtree(FFile user)/*删除子树 -结合 deletefile(); 使用 */ if

10、(user-brother!=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; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 20

11、页 - - - - - - - - - int a; 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(a1) p=user; user=user-brother; if(user-open=1)/ 判断

12、文件的开关情况 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; 名师资料总结 - - -精品资料欢迎下载 - - - - -

13、- - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 20 页 - - - - - - - - - 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) deletechildt

14、ree(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)/* 打开文件夹 */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 20 页

15、 - - - - - - - - - /int a,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;bopen=1;/ 设置文件为打开/strcpy(openfb.name,user-name); /openfb.add=user; printf( 文件夹打开成功。n); return(user);/

16、 返回被打开的文件夹的地址 void openfile(FFile user)/* 打开普通文件*/ if(user-open=1) printf( 错误:打开失败,此文件已经被打开!n); return; user-open=1; printf( 普通文件打开成功!n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 20 页 - - - - - - - - - FFile openff(FFile user)/* 打开文件 (夹)*/ char ch20; FFil

17、e parent; int a; 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;

18、if(user-type=1) printf( 开始打开文件夹。 。 。 n); user=openfolder(user); else if(user-type=2) printf( 开始打开普通文件。 。 。n); openfile(user); user=user-parent; return(user); void closefile(FFile user)/* 关闭普通文件 */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 8 页,共 20 页 - - - - - -

19、- - - 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=user-brother; if(user-open=0) printf( 错

20、误:关闭失败,该文件已经是关闭状态!n); return; user-open=0; printf( 文件已经成功关闭!); /* 没有文件夹关闭原因:文件夹一打开就会跳向打开的新文件里而文件夹关闭就会直接返回上一级的目录,若想整个文件夹都关闭,直接退出就可以了因此不会直接关闭某个特定的文件*/ FFile backf(FFile user)/*返回上一层目录*/ if(user-parent=NULL) printf( 错误:返回失败,此处是最顶层目录!n); return(user); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - -

21、 - - 名师精心整理 - - - - - - - 第 9 页,共 20 页 - - - - - - - - - user-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(!s

22、trcmp(user-name,ch) break; if(user-brother=NULL) printf( 错误:读取失败,当前位置没有该文件!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( 该文件内

23、没有可读内容!n); return; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页,共 20 页 - - - - - - - - - printf(%sn,user-iffile.ch); printf( 文件读取成功!n); void writefile(FFile user)/* 写文件 */ char ch20; int a; printf( 选择要进行写操作的文件名:n); scanf(%s,ch); if(user-child=NULL) printf( 错误:

24、写操作失败,该目录下没有可写的文件!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; else if(user-iffile.write=0) printf( 错误:文件写操作失败,该文件受保护,禁止写

25、!n); return; printf( 写操作 ,输入内容: n); scanf(%s,user-iffile.ch); user-iffile.length=strlen(user-iffile.ch); printf( 文件进行写操作成功!n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 20 页 - - - - - - - - - FFile copyfile(FFile user,FFile copyf)/* 拷贝文件 */ char ch20; int

26、 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; copyf=user; printf(

27、 拷贝成功! 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; 名师资料总结 - - -精品资料欢迎下载 -

28、 - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 20 页 - - - - - - - - - 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=fen

29、pei(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-iffil

30、e.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); void prastefile(FFile user,FFile copyf)/* 粘贴文件 */ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 20 页 - -

31、 - - - - - - - /user 是要粘贴的地方,copyf 是要粘贴的内容,/ 有相同文件名的会判断会不会覆盖,或者是重命名/ 在原树中进行新建操作 int i,j; char ch20; FFile p,user0,parent; parent=user;/ 记录父结点user=user-child; p=user;/ 记录当前结点的前一个brother 结点strcpy(ch,NULL); if(copyf=NULL)/ 判断有没有拷贝文件 printf( 错误:粘贴失败,还没有拷贝任何文件(夹)!n); return; /p=(Ffile*)malloc(sizeof(Ffil

32、e); /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-brother=NULL; user-parent=parent; return; / 该位置没有任何文件for(j=1;j+) if(user-type=0) / 开端的空结点,用新结点覆盖,即:当前位置没有文件结

33、点 user=user-parent; deletechildtree(p); user=fenpei(copyf,user-child,user);/返还增加的结点user-brother=NULL; user-parent=parent; parent-child=user; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 20 页 - - - - - - - - - return; if(!strcmp(user-name,copyf-name) printf(

34、提示:该文件名已经存在!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( 粘

35、贴成功。 n); 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) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 20 页 - - - - - - - -

36、- printf( 错误:属性修改失败,该目录下没有可修改的文件!n); return; printf( 选择要进行属性修改的文件(夹)名:n); scanf(%s,ch); user=user-child; for(a=1;a+)/ 找出要拷贝文件的所在位置,用user 替代 if(!strcmp(user-name,ch) break; if(user-brother=NULL) printf( 错误:属性修改失败,当前位置没有该文件!n); return; user=user-brother; if(user-type=1) printf( 错误:文件夹不能进行属性修改!n); retu

37、rn; for(;) printf(1. 修改读权限;n); printf(2. 修改写权限;n); printf(3. 返回; n); printf( 选择操作: n); scanf(%d,&a); if(a=1) printf(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; 名师资料总结 - -

38、-精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 16 页,共 20 页 - - - - - - - - - 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(*

39、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);/ 系统退出的操作码 else printf( 错误:没有该操作!n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 20 页 - - - - - -

40、- - - / 初始化打开列表/initopen(); / 初始化各个用户的信息/copyf=(Ffile*)malloc(sizeof(Ffile); /copyf=NULL; copyf=NULL; user1=init(); strcpy(user1-name,user1); user2=init(); strcpy(user2-name,user2); d=init(); e=init(); user1-child=d; user2-child=e; d-parent=user1; e-parent=user2; printf(%d,user1-child-type); if(b=1)

41、 printf( 已经进入user1 系统 n); f=user1; show(user1); else printf( 已经进入user2 系统 n); f=user2; show(user2); for(;) printf(*n); printf(1. 创建文件 (夹) 5.读文件9.显示当前路径n); printf(2. 删除文件 (夹) 6.写文件10.返回上一层目录n); printf(3. 打开文件 (夹) 7.拷贝文件11.改变普通文件属性n); printf(4. 关闭普通文件8.粘贴文件12.退出 n); printf(*n); printf( 选择操作: n); scanf

42、(%d,&c); if(c=12) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 20 页 - - - - - - - - - break; else if(c=1) creatf(f); else if(c=2) printf( 选择要删除的文件(夹)的名字: n); scanf(%s,ch); deletefile(f,ch); else if(c=3) f=openff(f); else if(c=4) closefile(f); else if(c=5) re

43、adfile(f); else if(c=6) writefile(f); else if(c=7) copyf=copyfile(f,copyf); else if(c=8) prastefile(f,copyf); copyf=NULL; else if(c=9) printf( 路径为: n); showroute(f); printf(n); else if(c=10) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 20 页 - - - - - - - - - f=backf(f); else if(c=11) change(f); else continue; show(f); printf( 欢迎使用 !n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 20 页 - - - - - - - - -

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号