资源预览内容
第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
亲,该文档总共6页全部预览完了,如果喜欢就下载吧!
资源描述
实验四 文件管理实验实验名称:文件管理实验仪器、设备:计算机参考资料:操作系统实验指导书实验目的:设计一个 n个用户的文件系统,用户在一次运行中只能打开一个文件,有Create、 delete、 open、close、read、write等命令。实验内容:为 DOS 系统设计一个简单的二级文件系统。要求可以实现下列几条命令CREATE 创建文件DELETE 删除文件OPEN 打开文件CLOSE 关闭文件READ 读文件WRITE 写文件实验原理、数据(程序)记录:#define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/#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 *filename); /*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 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 (fpaddrno=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);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 created 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;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)exist=1;break;if (exist) return(i);else return(-1);int FindPANo() /*find out physical address num*/int i;for(i=0;i=0)k=ExistD(username);if(ifopenki.ifopen=1)printf(nError. %s is in open status. Close it before delete.n,fname);wgetchar=1;elsewhile(flag)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-ufdfilej+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(nError. %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 ExistF(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);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;elseprintf(Please input FileOpenMode(0-Read Only,1-Write Only,2-Read and Write):);gets(fmode);ltrim(rtrim(fmode);if(strcmp(fmode,0)=0)|(strcmp(fmode,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.ifopen=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);if(ifopenki
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号