2022年操作系统文件管理实验 .pdf
《2022年操作系统文件管理实验 .pdf》由会员分享,可在线阅读,更多相关《2022年操作系统文件管理实验 .pdf(9页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验五文件管理实验实验名称:文件管理实验仪器、设备:计算机参考资料:操作系统实验指导书实验目的:设计一个n 个用户的文件系统,用户在一次运行中只能打开一个文件,有Create、delete、open、close、read、write等命令。实验内容:为 DOS 系统设计一个简单的二级文件系统。要求可以实现下列几条命令CREATE 创建文件DELETE 删除文件OPEN 打开文件CLOSE 关闭文件READ 读文件WRITE 写文件实验原理、数据(程序)记录:#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/
2、#define MAXCHILD 50/*the largest child*/#define MAX(MAXCHILD*MAXCHILD)/*the size of fpaddrno*/void CreateF()/*Create File*/int fpaddrno,flag=1,i;char fnameMAXNAME,str50,str150,strtext255,a25;char fmode25;int FindPANo();/*find out physical address num*/int WriteF1();/*write file*/int ExistF(char*file
3、name);/*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char*dirname);if(strcmp(strupr(dirname),strupr(username)!=0)printf(nError.You must create file in your own dir.n);wgetchar=1;else printf(nPlease input FileName:);gets(fname);ltrim(rtrim(fname);if(ExistF(fname)=0)printf(nError.Name%s has
4、already existed.n,fname);wgetchar=1;else printf(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)名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 9 页 -|(strcmp(fmode,3)=0)fpaddrno=FindPANo();if(fpad
5、drno=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,c:osfilefilefile);itoa(fpaddrno,str1,10);strcat(str,str1);fp_file=fopen(str,wb);fclose(fp_file);fco
6、unti+;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 created successfully!n,fname);else p
7、rintf(nFail!No Disk Space.Please format your disk.n);wgetchar=1;else printf(nError.FileModes Range is 0-3n);wgetchar=1;int ExistF(char*filename)/*Whether FileName Exist,Exist-i,Not Exist-0*/int i,j;int exist=0;int ExistD(char*dirname);j=ExistD(dirname);for(i=0;iufdfilei.fname),strupr(filename)=0)exi
8、st=1;break;if(exist)return(i);else return(-1);名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 9 页 -int FindPANo()/*find out physical address num*/int i;for(i=0;iMAX;i+)if(fpaddrnoi=0)fpaddrnoi=1;break;if(i=0)k=ExistD(username);if(ifopenki.ifopen=1)printf(nError.%s is in open status.Close it before delete.n,fname);wg
9、etchar=1;else while(flag)名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 9 页 -printf(%s will be deleted.Are you sure(Y/N):,fname);gets(a);ltrim(rtrim(a);if(strcmp(strupr(a),Y)=0)fpaddrnoufdk-ufdfilei.fpaddr=0;itoa(ufdk-ufdfilei.fpaddr,str,10);for(j=i;jufdfilej.fname,ufdk-ufdfilej+1.fname);ufdk-ufdfilej.fpaddr=ufdk-u
10、fdfilej+1.fpaddr;ufdk-ufdfilej.flength=ufdk-ufdfilej+1.flength;ufdk-ufdfilej.fmode=ufdk-ufdfilej+1.fmode;ifopenkj=ifopenkj+1;fcountk-;strcpy(str1,c:osfilefilefile);strcat(str1,str);remove(str1);flag=0;printf(n%s has been deleted successfully.n,fname);wgetchar=1;else if(strcmp(strupr(a),N)=0)printf(n
11、Error.%s hasnt been deleted.n,fname);wgetchar=1;flag=0;else printf(nError.%s does not exist.n,fname);wgetchar=1;*void OpenF()/*Open File*/char fnameMAXNAME;char str25,str125,fmode25;int i,k;char*rtrim(char*str);/*remove the trailing blanks.*/char*ltrim(char*str);/*remove the heading blanks.*/int Exi
12、stF(char*filename);/*Whether FileName Exist,Exist-i,Not Exist-0*/int ExistD(char*dirname);if(strcmp(strupr(ltrim(rtrim(dirname),)=0)printf(nError.Please change to ufd dir before open.n);wgetchar=1;return;printf(nPlease input FileName:);gets(fname);ltrim(rtrim(fname);i=ExistF(fname);名师资料总结-精品资料欢迎下载-名
13、师精心整理-第 4 页,共 9 页 -if(i=0)k=ExistD(dirname);if(!ifopenki.ifopen)if(ufdk-ufdfilei.fmode=3)printf(nError.The files mode is FORBID.Can not open.n);wgetchar=1;else printf(Please input FileOpenMode(0-Read Only,1-Write Only,2-Read and Write):);gets(fmode);ltrim(rtrim(fmode);if(strcmp(fmode,0)=0)|(strcmp(f
14、mode,1)=0)|(strcmp(fmode,2)=0)if(fmode0=0)/*open file with read only mode*/strcpy(str,read only);if(ufdk-ufdfilei.fmode=0)|(ufdk-ufdfilei.fmode=2)ifopenki.ifopen=1;else if(fmode0=1)/*open file with write only mode*/strcpy(str,write only);if(ufdk-ufdfilei.fmode=1)|(ufdk-ufdfilei.fmode=2)ifopenki.ifop
15、en=1;else if(fmode0=2)/*open file with read and write mode*/strcpy(str,read and write);if(ufdk-ufdfilei.fmode=2)ifopenki.ifopen=1;if(ufdk-ufdfilei.fmode=0)strcpy(str1,read only);/*FileMode*/else if(ufdk-ufdfilei.fmode=1)strcpy(str1,write only);else if(ufdk-ufdfilei.fmode=2)strcpy(str1,read and write
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年操作系统文件管理实验 2022 操作系统 文件 管理 实验
限制150内