实验四文件管理实验.doc_第1页
实验四文件管理实验.doc_第2页
实验四文件管理实验.doc_第3页
实验四文件管理实验.doc_第4页
实验四文件管理实验.doc_第5页
已阅读5页,还剩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*/#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; 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)|(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;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);wgetchar=1; else while(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; 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(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.ifopen=1)ifopenki.openmode=atoi(fmode); if (ifopenki.openmode=0) strcpy(str,read only); else if(ifopenki.openmode=1) strcpy(str,write only); else if(ifopenki.openmode=2) strcpy(str,read and write); printf(n%s has been opened. OpenMode is %s,FileMode is %sn,fname,strupr(str),strupr(str1); wgetchar=1; else printf(nError. %s hasnt been opened. OpenMode Error. OpenMode is %s,but FileMode is %sn,fname,strupr(str),strupr(str1);wgetchar=1; else printf(nError. FileOpenModes Range is 0-2n);wgetchar=1; else printf(nError. %s is in open status.n,fname);wgetchar=1; else printf(nError. %s does not exist.n,fname);wgetchar=1;*void CloseF() /*Close File*/int i,k,n=0; char fnameMAXNAME; 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 convert to ufd dir before close.n);wgetchar=1;return; k=ExistD(dirname); printf(nOpen File(s) In This Ufd:n);/*display openned file*/ 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) ifopenki.ifopen=0; ifopenki.openmode=4; printf(n%s has been closed successfully.n,fname); wgetchar=1; else printf(nError.%s is in closing status.n,fname);wgetchar=1; else printf(nError. %s is not exist.n,fname);wgetchar=1;*void ReadF() /*Read File*/int i,k,n=0; char fnameMAXNAME; char str255,str1255,c; 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 convert to ufd dir before read.n);wgetchar=1;return; printf(nCaution:Open file firstn); printf(Opened File(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.openmode=2) itoa(ufdk-ufdfilei.fpaddr,str,10); strcpy(str1,file); strcat(str1,str); strcpy(str,c:osfilefile); strcat(str,str1); 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,c); printf(nn%d Length.n,ufdk-ufdfilei.flength); fclose(fp_file); wgetchar=1; elseprintf(nError.%s has been opened with WRITE ONLY mode. It isnt read.n,fname);wgetchar=1; else printf(nError.%s is in closing status. Please open it before readn,fname);wgetchar=1; else printf(nError. %s does not exist.n,fname);wgetchar=1;*void WriteF() /*Write File*/int i,k,n=0; char fnameMAXNAME; char str50,str150,a50; 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); int WriteF1(); /*write file*/ if (strcmp(strupr(ltrim(rtrim(dirname),)=0) printf(nError. Please convert to ufd dir before write.n);wgetchar=1;return; k=ExistD(dirname); printf(nOpen File(s) with write only mode or read and write mode:n);/*display openned files with writable mode*/ for(i=0;iufdfilei.fname);n+;if(n%4=0)&(n!=0) printf(n); printf(n%d files open.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,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论