版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、 北京联合大学 信息学院 计 算 机 操 作 系 统 实 训 报 告 实 验 序 号:003 姓 名: 学 号: 班 级: 专 业:一:实验题目:模拟文件管理二:设计思想: 文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件。首先应确定文件系统的数据结构:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记。实现功能:login 用户登录 dir 列目录 modify 修改文件属性 create 创建文件 delete
2、删除文件 open 打开文件 close 关闭文件 read 读文件 write 写文件 cd 更改目录三:数据结构typedef struct /*定义文件*/int fpaddr; /*文件地址*/ int flength; /*文件长度*/ int fmode; /*文件属性:0-Read Only;1-Write Only;2-Read and Write; 3-Protect;*/ char fnameMAXNAME; /*文件名*/ OSFILE;typedef struct /*the structure of OSUFD定义用户文件目录*/char ufdnameMAXNAME
3、; /*ufd name*/OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFDLOGIN定义登陆*/char ufdnameMAXNAME; /*ufd name*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode定义操作方式*/int ifopen; /*ifopen:0-close,1-open*/ int openmode; /*0-read only,1-writ
4、e only,2-read and write,3-protect*/OSUFD_OPENMODE;四:定义函数void LoginF(); /*登录文件系统*/void DirF(); /*显示系统目录*/void CreateF(); /*创建文件*/void DeleteF(); /*删除文件*/void ModifyFM(); /*修改文件属性*/void OpenF(); /*打开文件*/void CloseF(); /*关闭文件*/void ReadF(); /*读文件*/void WriteF(); /*写文件*/void QuitF(); /*退出*/void CdF(); /
5、*更改目录名称*/void help();登录函数void LoginF() /*LOGIN FileSystem登陆函数*/char loginameMAXNAME,loginpw9,logincpw9,str50;/loginpw-输入密码,logincpw-确认输入密码,str50存放地址int i,j,flag=1;char a25;int findout; /*login user not exist*/while(1)findout=0;printf(nnLogin Name:);gets(loginame);/把用户名写入系统ltrim(rtrim(loginame);fp_mf
6、d=fopen(d:osfilemfd.txt,rb);/打开文件mfdfor(i=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;i+)if (strcmp(strupr(ufd_lp.ufdname),strupr(loginame)=0)/两者相等findout=1;strcpy(logincpw,ufd_lp.ufdpword);fclose(fp_mfd);/关闭mfd文件if (findout=1) /*user exist该用户存在*/printf(Login Password:);/*用户密码输入*/InputPW(loginp
7、w); /*input password,use * replace*/if (strcmp(loginpw,logincpw)=0)/相等strcpy(username,strupr(loginame);strcpy(dirname,username);fp_mfd=fopen(d:osfilemfd.txt,rb);for(j=0;fread(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd)!=0;j+)/记录用户名strcpy(str,d:osfile);strcat(str,ufd_lp.ufdname);/*strcat(dest,char)添加char到d
8、est结尾处*/strcat(str,.txt);ufdj=(OSUFD*)malloc(sizeof(OSUFD);/malloc向系统申请分配指定size个字节的内存空间分配二级文件空间strcpy(ufdj-ufdname,strupr(ufd_lp.ufdname);fp_ufd=fopen(str,rb);fcountj=0;/当前用户文件数目for(i=0;fread(&ufdj-ufdfilei,sizeof(OSFILE),1,fp_ufd)!=0;i+,fcountj+)/记录用户文件ifopenji.ifopen=0;ifopenji.openmode=4;fclose(f
9、p_ufd);fclose(fp_mfd);ucount=j;SetPANo(0);printf(nnLogin successful! Welcome to this FileSystemnn);loginsuc=1;return;else/不相等printf(nn);flag=1;while(flag)printf(Login Failed! Password Error. Try Again(Y/N):);gets(a);ltrim(rtrim(a);if (strcmp(strupr(a),Y)=0) loginsuc=0;flag=0;else if(strcmp(strupr(a)
10、,N)=0)loginsuc=0;flag=0;return;else/*user not exist*/printf(New Password(=8):);InputPW(loginpw); /*input new password,use * replace*/printf(nConfirm Password(ufdname,strupr(ufd_lp.ufdname);fp_ufd=fopen(str,rb);for(i=0;fread(&ufdj-ufdfilei,sizeof(OSFILE),1,fp_ufd)!=0;i+,fcountj+)/记录用户文件ifopenji.ifope
11、n=0;ifopenji.openmode=4;fclose(fp_ufd);fclose(fp_mfd);ucount=j;SetPANo(0);printf(nnLogin Successful! Welcome to this Systemnn);loginsuc=1;return;else/不相等printf(nn);flag=1;while(flag)printf(Login Failed! Password Error. Try Again(Y/N):);gets(a);ltrim(rtrim(a);if (strcmp(strupr(a),Y)=0) loginsuc=0;fla
12、g=0;else if(strcmp(strupr(a),N)=0)loginsuc=0;flag=0;return;显示系统目录void DirF() /*Dir FileSystem主目录*/int i,j,count=0;char sfmode25,sfpaddr25,str25;clrscr();if (strcmp(strupr(ltrim(rtrim(dirname),)!=0)/不为空printf(nnd:%sdirn,dirname);printf(n%14s%16s%14s%10s%18sn,FileName,FileAddress,FileLength,Type,FileM
13、ode);j=ExistD(dirname);for(i=0;iufdfilei.fpaddr,str,10);/itoa()把整数转换为字符串strcpy(sfpaddr,file);strcat(sfpaddr,str);if (ufdj-ufdfilei.fmode=0) strcpy(sfmode,Read Only);else if(ufdj-ufdfilei.fmode=1) strcpy(sfmode,Write Only);else if(ufdj-ufdfilei.fmode=2)strcpy(sfmode,Read And Write);else strcpy(sfmode
14、,Protect);printf(%14s%16s%14d%10s%18sn,ufdj-ufdfilei.fname,sfpaddr,ufdj-ufdfilei.flength,sfmode);printf(n %3d file(s)n,fcountj);else/空printf(nnd:dirn);printf(n%14s%18s%8sn,DirName,OwnFileCount,Type);for(i=0;iufdname,fcounti,);count=count+fcounti;printf(n %3d dir(s),%5d file(s)n,ucount,count);创建文件voi
15、d CreateF() /*Create File*/int fpaddrno,flag=1,i;char fnameMAXNAME,str50,str150,a25;char fmode25;if (strcmp(strupr(dirname),strupr(username)!=0)/目录名与用户名不相等printf(nError. You must create file in your own dir.n);wgetchar=1;elseprintf(nPlease input FileName:);gets(fname);ltrim(rtrim(fname);if (ExistF(f
16、name)=0)printf(nError. Name %s has already existed.n,fname);wgetchar=1;elseprintf(Please input FileMode(0-Read Only, 1-Write Only, 2-Read and Write, 3-Protect):);gets(fmode);ltrim(rtrim(fmode);if(strcmp(fmode,0)=0)|(strcmp(fmode,1)=0)|(strcmp(fmode,2)=0)|(strcmp(fmode,3)=0)fpaddrno=FindPANo();if (fp
17、addrno=0)/找地址i=ExistD(username);strcpy(ufdi-ufdfilefcounti.fname,fname);/记录ufdi-ufdfilefcounti.fpaddr=fpaddrno;ufdi-ufdfilefcounti.fmode=atoi(fmode);ifopenifcounti.ifopen=0;ifopenifcounti.openmode=4;strcpy(str,d:osfilefilefile);itoa(fpaddrno,str1,10);strcat(str,str1);strcat(str,.txt);fp_file=fopen(s
18、tr,wb);fclose(fp_file);fcounti+;while(flag)printf(Input text now(Y/N):);gets(a);ltrim(rtrim(a);ufdi-ufdfilefcounti-1.flength=0;if(strcmp(strupr(a),Y)=0)fp_file=fopen(str,wb+);/写入二进制文件ufdi-ufdfilefcounti-1.flength=WriteF1();flag=0;else if(strcmp(strupr(a),N)=0)flag=0;wgetchar=1;printf(n%s has been cr
19、eated successfully!n,fname);elseprintf(nFail!No Disk Space. Please format your disk.n);wgetchar=1;else printf(nError. FileModes Range is 0-3n);wgetchar=1;删除文件:void DeleteF() /*Delete File*/ char fnameMAXNAME,str50,str150;int i,k,j;int fpaddrno1; if (strcmp(strupr(ltrim(rtrim(dirname),)=0) /*无法删除主目录的
20、文件*/printf(nError.Please convert to ufd dir before delete.n);wgetchar=1;if (strcmp(strupr(dirname),strupr(username)!=0) /*无法删除非自己目录的文件*/printf(nError.You can only modify filemode in yourself dir.n);wgetchar=1;elseprintf(nPlease input FileName:);gets(fname);/从键盘获取所要删除的文件名ltrim(rtrim(fname);i=ExistF(f
21、name);/获取文件编号if (i=0)k=ExistD(username);if(ifopenki.ifopen=1)/*文件打开时无法删除*/printf(nError.%s is in open status. Close it before delete.n,fname);wgetchar=1;else if(ufdk-ufdfilei.fmode=3)/*被保护的文件无法删除*/ printf(nError.%s is in protect status.The file can not be deleted!.n,fname);wgetchar=1; else fpaddrno1
22、=ufdk-ufdfilei.fpaddr;/获取文件对应的物理文件名fpaddrnofpaddrno1=0;/回收盘块for(j=i;jufdfilej=ufdk-ufdfilej+1;/从被删除的文件号开始,数组值全部前移一个 strcpy(str,d:osfilefilefile);itoa(fpaddrno1,str1,10);/整数转化成字符串strcat(str,str1);strcat(str,.txt);remove(str);/删除物理文件fcountk-;/用户文件数量少1printf(n %sis deleted successfully.n,fname);wgetcha
23、r=1;else/所要删除的文件不存在printf(nError. %s dose not exist.n,fname);wgetchar=1; 修改文件属性void ModifyFM() /*Modify FileMode*/char fnameMAXNAME,str50;int i,k;char fmode25; /*whether delete*/if (strcmp(strupr(dirname),strupr(username)!=0) printf(nError.You can only modify filemode in yourself dir.n);wgetchar=1;e
24、lse printf(nPlease input FileName:);gets(fname);ltrim(rtrim(fname);i=ExistF(fname);if (i=0)k=ExistD(username);if(ifopenki.ifopen=1)printf(nError.%s is in open status. Close it before modify.n,fname);wgetchar=1;elseif(ufdk-ufdfilei.fmode=0) strcpy(str,read only); /*FileMode*/else if(ufdk-ufdfilei.fmo
25、de=1) strcpy(str,write only);else if(ufdk-ufdfilei.fmode=2) strcpy(str,read and write);else strcpy(str,Protect);printf(%s filemode is %s.n,fname,strupr(str);printf(Modify to(0-read only,1-write only,2-read and write,3-Protect):);gets(fmode);ltrim(rtrim(fmode);if(strcmp(fmode,0)=0)ufdk-ufdfilei.fmode
26、=0;printf(n%s has been modified to READ ONLY mode successfully.n,fname);wgetchar=1;else if(strcmp(fmode,1)=0)ufdk-ufdfilei.fmode=1;printf(n%s has been modified to WRITE ONLY mode successfully.n,fname);wgetchar=1;else if(strcmp(fmode,2)=0)ufdk-ufdfilei.fmode=2;printf(n%s has been modified to READ AND
27、 WRITE mode successfully.n,fname);wgetchar=1;else if(strcmp(fmode,3)=0)ufdk-ufdfilei.fmode=3;printf(n%s has been modified to FORBID mode successfully.n,fname);wgetchar=1;else printf(nError.%s is not modified.n,fname);wgetchar=1;elseprintf(nError. %s dose not exist.n,fname);wgetchar=1;打开文件void OpenF(
28、) /*Open File*/ char fnameMAXNAME;int i,k,j; if (strcmp(strupr(dirname),strupr(username)!=0) /*在自己的目录里才能打开*/printf(nError.You can only open in yourself dir.n);wgetchar=1;else k=ExistD(username); for(j=0;jufdfilej.fname); printf(nPlease input FileName:);gets(fname);/获取所要打开的文件名ltrim(rtrim(fname);i=Exi
29、stF(fname);/获取目录编号if (i=0)if(ifopenki.ifopen=1)/输入的文件是打开的printf(nError.%s is in open status.n,fname);wgetchar=1;else ifopenki.ifopen=1;/修改为打开的if(ufdk-ufdfilei.fmode=0)/*根据文件的模式设置打开模式*/ ifopenki.openmode=0;else if(ufdk-ufdfilei.fmode=1)ifopenki.openmode=1;else if(ufdk-ufdfilei.fmode=2) ifopenki.openm
30、ode=2;else ifopenki.openmode=3;printf(n%s is opened successfullyn,fname);wgetchar=1;else/文件不存在printf(nError. %s dose not exist.n,fname);wgetchar=1;关闭文件void CloseF() /*Close File*/char fnameMAXNAME;int i,k,j; if (strcmp(strupr(dirname),strupr(username)!=0) /*不在自己的目录里没法进行*/printf(nError.You can only c
31、lose file in yourself dir.n);wgetchar=1;else k=ExistD(username); for(j=0;jufdfilej.fname); printf(nPlease input FileName:);gets(fname);/从键盘上读入所要关闭的文件ltrim(rtrim(fname);i=ExistF(fname);/获取文件编号if (i=0)ifopenki.ifopen=0;/关闭文件printf(n %s closed successfullyn,fname);wgetchar=1;else/所要关闭的文件不存在printf(nErro
32、r. %s dose not exist.n,fname);wgetchar=1;读文件void ReadF() /*Read File*/int i,k,n=0;char fnameMAXNAME;char str255,str1255,c;if (strcmp(strupr(dirname),strupr(username)!=0)/必须在自己的目录内printf(nError.Please convert to your own dir before read.n);return;elseprintf(nCaution:Open file firstn); printf(Opened F
33、ile(s) List:n); k=ExistD(dirname);for(i=0;iufdfilei.fname);n+;if(n%4=0)&(n!=0) printf(n);printf(n%d files openned.n,n);if (n=0) wgetchar=1;/如果没有打开的文件if(n!=0)printf(nPlease input FileName:);gets(fname);ltrim(rtrim(fname);i=ExistF(fname);if(i=0)if(ifopenki.ifopen=1)if(ifopenki.openmode=0) |(ifopenki.o
34、penmode=2)/如果文件只读或者读写itoa(ufdk-ufdfilei.fpaddr,str,10); strcpy(str1,file); strcat(str1,str); strcpy(str,d:osfilefile); strcat(str,str1); strcat(str,.txt); fp_file=fopen(str,rb); fseek(fp_file,0,0); printf(nThe text is:nn); printf( );while(fscanf(fp_file,%c,&c)!=EOF)if (c=n) printf(n );else printf(%c
35、,c);printf(nn%d Length.n,ufdk-ufdfilei.flength);fclose(fp_file);else if(ifopenki.openmode=1)/如果文件只写printf(nError.%s has been opened with WRITE ONLY mode. It isnt read.n,fname);else/如果文件保护printf(nError.%s has been opened with PROTECT mode. It isnt read.n,fname);else printf(nError.%s is in closing sta
36、tus. Please open it before readn,fname);elseprintf(nError. %s does not exist.n,fname);写文件void WriteF() /*Write File*/int i,k,n=0;int length;char fnameMAXNAME,values1000;char str255,str1255,c;if (strcmp(strupr(dirname),strupr(username)!=0) /只能写自己目录下的文件printf(nError.Please convert to your own dir befo
37、re write.n);elsek=ExistD(dirname);/获取目录编号printf(nCaution:Open file firstn); printf(Opened File(s) List:n); for(i=0;iufdfilei.fname);n+;if(n%4=0)&(n!=0) printf(n);printf(n%d files openned.n,n);if (n=0) wgetchar=1;/没有打开的文件if(n!=0)printf(nPlease input FileName:);gets(fname);/从键盘获取所要写的文件ltrim(rtrim(fname);i=ExistF(fname);/获取文件编号if(i=0)if(ifopenki.ifopen=1)/如果文件是打开if(ifopenki.openmode=1) |(ifopenki.openmode=2)/文件是只写,或者是可读可写的itoa(ufdk-ufdfilei.fpaddr,str,10);/获取文件对应的物理地址 strcpy(str1,file); strcat(str1,str); strcpy(st
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年全国人大代表建言:推动碳资产变资本加快建设统一碳市场
- 2026年生物质电厂设备维护检修标准化手册
- 2026届浙江省宁波市海曙区三校联考初三第二学期综合练习(一)化学试题含解析
- 2026届福建省郊尾、枫亭五校教研小片区市级名校初三下学期第一次阶段考试(5月)化学试题含解析
- 辽宁省辽阳县重点名校2026届广东中考全真生物试题模拟试卷含解析
- 2026年广西南宁市天桃实验校联盟测试化学试题含解析
- 四川省乐山市2026年初三第二次调查研究考试化学试题含解析
- 云南省涧南彝族自治县市级名校2026届初三第三次月考化学试题含解析
- 2026年江西省抚州市宜黄县达标名校下学期初三化学试题第三次统一练习试题含解析
- 2026年液晶电视机开关电源电路故障快速诊断
- 2026年六安职业技术学院单招职业适应性考试题库附答案详解(预热题)
- 2026天津市津南区事业单位招聘37人考试参考试题及答案解析
- 2026年南京机电职业技术学院单招职业适应性测试题库(含答案详解)
- 2026年春节后复工复产“开工第一课”安全生产培训课件
- 专题学习《改革开放简史》
- 地下车库消防系统施工方案
- 灵活用工人员安全培训课件
- 用电安全进校园宣传课件
- 2026年中国速冻水饺市场运行(产业链、市场规模、价格等)现状及未来发展趋势分析
- (新教材)2026年人教版一年级下册数学 第二单元 20以内的退位减法 整 理和复习 课件
- 2026年无锡科技职业学院单招综合素质考试必刷测试卷必考题
评论
0/150
提交评论