实验二文件系统模拟设计.docx

上传人:小飞机 文档编号:3436422 上传时间:2023-03-13 格式:DOCX 页数:14 大小:40.31KB
返回 下载 相关 举报
实验二文件系统模拟设计.docx_第1页
第1页 / 共14页
实验二文件系统模拟设计.docx_第2页
第2页 / 共14页
实验二文件系统模拟设计.docx_第3页
第3页 / 共14页
实验二文件系统模拟设计.docx_第4页
第4页 / 共14页
实验二文件系统模拟设计.docx_第5页
第5页 / 共14页
亲,该文档总共14页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《实验二文件系统模拟设计.docx》由会员分享,可在线阅读,更多相关《实验二文件系统模拟设计.docx(14页珍藏版)》请在三一办公上搜索。

1、实验二 文件系统模拟设计实验二 文件系统模拟设计 一、实验目的 通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。 二、实验内容 为linux系统设计一个简单的二级文件系统。要求做到以下几点: 可以实现下列几条命令; login 用户登录 dir 列文件目录 create 创建文件 delete 删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件 列目录时要列出文件名、物理地址、保护码和文件长度; 源文件可以进行读写保 三、实验内容指导提示 首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁

2、盘,这样便于查找和修改。 用户创建的文件,可以编号存储于磁盘上。如file0,file1,file2.并以编号作为物理地址,在目录中进行登记。 程序设计思想参考 (1)设计思想 本系统是模拟实现多用户多目录的文件系统,在系统出现登录后 ,输入用户与口令,在用户登录系统后,可建立文件卷,将用户输入的文件保存在指定的文件中。系统的命令与其命令的具体实现,此模拟系统共提供了上述命令,并根据命令的含义与要求,用C+编程来完成所有具体操作。该系统可以模拟完成用户的登陆和验证,列出文件和目录,新建目录,改变目录,创立和编写文件,删除文件和退出系统等功能。 (2)主要数据结构 用户结构:账号与密码结构 ty

3、pedef struct users char name8; char pwd10; users; 本系统有8个默认的用户名,前面是用户名,后面为密码,用户登陆时只要输入正确便可进入系统,否则提示失败要求重新输入。 users usrarray8 = usr1,usr1, usr2,usr2, usr3,usr3, usr4,usr4, usr5,usr5, usr6,usr6, usr7,usr7, usr8,usr8, ; (3)数据结构说明 a)文件结构链表 struct fnode char filenameFILENAME_LENGTH; int isdir; int isopen;

4、 char content255; fnode *parent; fnode *child; fnode *prev; fnode *next; ; b)函数介绍 fnode *initfile(char filename,int isdir);/初始化文件或目录 void createroot;/建立系统根目录 int run;系统运行 int findpara(char *topara);对参数进行处理 bool chklogin(char *users, char *pwd);检查账号与口令 void help;命令列表 int mkdir;建立目录 int create;建立文件 in

5、t read;读取文件 int write;写入文件 int del;删除文件 int cd;切换目录 int dir;文件与目录列表 (4)各模块流程图 (5)、模拟文件系统参考程序清单 #include stdio.h #include iostream.h #include string.h #include iomanip.h #define FILENAME_LENGTH 10 /文件名称长度 #define COMMAND_LENGTH 10 /命令行长度 #define PARA_LENGTH 30 /参数长度 /账号结构 typedef struct users char na

6、me8; char pwd10; users; /文件结构 struct fnode char filenameFILENAME_LENGTH; int isdir; int isopen; char content255; fnode *parent; fnode *child; fnode *prev; fnode *next; ; /账号 users usrarray8 = usr1,usr1, usr2,usr2, usr3,usr3, usr4,usr4, usr5,usr5, usr6,usr6, usr7,usr7, usr8,usr8, ; fnode *initfile(ch

7、ar filename,int isdir); void createroot; int run; int findpara(char *topara); bool chklogin(char *users, char *pwd); void help; int mkdir; int create; int read; int write; int del; int cd; int dir; fnode *root,*recent,*temp,*ttemp; char paraPARA_LENGTH,commandCOMMAND_LENGTH,tempparaPARA_LENGTH,recen

8、tparaPARA_LENGTH; /创建文件与目录结点 fnode* initfile(char filename,int isdir) fnode *node=new fnode; strcpy(node-filename,filename); node-isdir=isdir; node-isopen=0; node-parent=NULL; node-child=NULL; node-prev=NULL; node-next=NULL; return node; /创建文件存储结点 void createroot recent=root=initfile(/,1); root-pare

9、nt=NULL; root-child=NULL; root-prev=root-next=NULL; strcpy(para,/); int mkdir temp=initfile( ,1); cintemp-filename; if(recent-child=NULL) temp-parent=recent; temp-child=NULL; recent-child=temp; temp-prev=temp-next=NULL; else ttemp=recent-child; while(ttemp-next) ttemp=ttemp-next; if(strcmp(ttemp-fil

10、ename,temp-filename)=0&ttemp-isdir=1) printf(对不起,目录已存在!); return 1; ttemp-next=temp; temp-parent=NULL; temp-child=NULL; temp-prev=ttemp; temp-next=NULL; return 1; int create temp=initfile( ,0); cintemp-filename; cintemp-content; if(recent-child=NULL) temp-parent=recent; temp-child=NULL; recent-child

11、=temp; temp-prev=temp-next=NULL; cout文件建立成功!child; while(ttemp-next) ttemp=ttemp-next; if(strcmp(ttemp-filename,temp-filename)=0&ttemp-isdir=0) printf(对不起,文件已存在!); return 1; ttemp-next=temp; temp-parent=NULL; temp-child=NULL; temp-prev=ttemp; temp-next=NULL; cout文件建立成功!endl; return 1; int dir int i=

12、0,j=0; temp=new fnode; temp=recent; if(temp!=root) cout .child=NULL) coutTotal: directors i files j child; while(temp) if(temp-isdir) cout filenameendl;i+; else cout filenamenext; coutTotal: directors i files j filename; if(recent-child=NULL) cout文件不存在!child-filename,filename)=0) coutchild-contentch

13、ild; while(temp-next) if(strcmp(temp-next-filename,filename)=0) coutnext-contentendl; return 1; cout文件不存在!filename; if(recent-child=NULL) cout文件不存在!child-filename,filename)=0) recent-child-isopen=1;/设置文件标记为打开 cinrecent-child-content; recent-child-isopen=0;/设置文件标记为关闭 cout文件写入成功!child; while(temp-next

14、) if(strcmp(temp-next-filename,filename)=0) recent-child-isopen=1;/设置文件标记为打开 cintemp-next-content; recent-child-isopen=0;/设置文件标记为关闭 cout文件写入成功!endl; return 1; cout文件不存在!topara; if(strcmp(topara,.)=0) int i; while(recent-prev) recent=recent-prev; if(recent-parent) recent=recent-parent; i=strlen(para)

15、; while(parai!=/ & i0) i-; if(i!=0) parai=0; else parai+1=0; else findpara(topara); return 1; int findpara(char *topara) int i=0; int sign=1; if(strcmp(topara,/)=0) recent=root; strcpy(para,/); return 1; temp=recent; strcpy(temppara,para); if(topara0=/) recent=root-child; i+; strcpy(para,/); else if

16、(recent!=NULL & recent!=root) strcat(para,/); if(recent & recent-child) if(recent-isdir) recent=recent-child; else printf(路径错误!n); return 1; while(ichild) i+; if(recent-isdir) recent=recent-child; else printf(路径错误n); return 0; strcat(para,/); while(toparai!=/ & ifilename,recentpara)!=0 | (recent-isd

17、ir!=1) & recent-next!=NULL) recent=recent-next; if(strcmp(recent-filename,recentpara)=0) if(recent-isdir=0) strcpy(para,temppara); recent=temp; printf(是文件不是目录。n); return 0; strcat(para,recent-filename); if(strcmp(recent-filename,recentpara)!=0 | recent=NULL) strcpy(para,temppara); recent=temp; print

18、f(输入路径错误n); return 0; return 1; int del char filenameFILENAME_LENGTH; cinfilename; temp=new fnode; if(recent-child) temp=recent-child; while(temp-next & (strcmp(temp-filename,filename)!=0 | temp-isdir!=0) temp=temp-next; if(strcmp(temp-filename,filename)!=0) cout不存在该文件!endl; return 0; else cout不存在该文

19、件!parent=NULL) temp-prev-next=temp-next; if(temp-next) temp-next-prev=temp-prev; temp-prev=temp-next=NULL; else if(temp-next) temp-next-parent=temp-parent; temp-parent-child=temp-next; delete temp; cout文件已删除!endl; bool chklogin(char *users, char *pwd) int i; for(i=0; i8; i+) if( (strcmp(users,usrarr

20、ayi.name)=0) & (strcmp(pwd,usrarrayi.pwd)=0) return true; return false; void help(void) cout 命 令 一 览 endl; coutendl; coutcreate: 建立文件。 endl; coutread: 读取文件。 endl; coutwrite: 写入文件,支持多线程 endl; coutdel : 删除文件。 endl; coutmkdir: 建立目录。 endl; coutcd: 切换目录。 endl; coutlogout: 退出登录。 endl; int run coutlinux:pa

21、ra; cincommand; if(strcmp(command,mkdir)=0) mkdir; else if(strcmp(command,dir)=0) dir; else if(strcmp(command,cd)=0) cd; else if(strcmp(command,create)=0) create; else if(strcmp(command,read)=0) read; else if(strcmp(command,write)=0) write; else if(strcmp(command,del)=0) del; else if(strcmp(command,

22、help)=0) help; else if(strcmp(command,logout)=0) return 0; else cout请参考help提供的命令列表!endl; int main int i=0; bool in=false; char users8,pwd12; cout|-|endl; cout| c语言模拟Linux文件系统 |endl; cout| 账号:usr1-usr8 密码:usr1-usr8 |endl; cout| 你只有三次机会来试验账号 |endl; cout| 键入help可以获取帮助 |endl; cout|_|endl; coutendl; while(i3) coutusers; coutpwd; if(chklogin(users,pwd) in=true;break; i+; createroot; while(in) if(!run) break; 六、实验实习或教学实习报告要求 七、实验实习成绩评定方式 根据学生的学习态度、上机完成结果等按“A、B、C、D”综合考核评定或按百分制评定。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号