《2022年操作系统文件操作 .pdf》由会员分享,可在线阅读,更多相关《2022年操作系统文件操作 .pdf(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1 实验四文件操作一、实验名称:简单文件系统的实现二、实验目的要求在 TC、VB 、Delphi 、C+Builder等语言与开发环境中,实现对文件的各项操作(复制、打开、保存、录入、查找、显示属性等);使学生进一步了解文件的主要操作。三、实验内容为 DOS 系统设计一个简单的二级文件系统。要求做到以下几点:可以实现下列几条命令LOGIN 用户登陆DIR 列文件目录CREATE 创建文件DELETE 删除文件OPEN 打开文件CLOSE 关闭文件READ 读文件WRITE 写文件MODIFY 修改文件内容列目录时要列出文件名,物理地址,保护码和文件长度。源文件可以进行读写保护。四、实验提示首先
2、应确定文件系统的数据结构:主目录、子目录及活动文件等。 主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记。五、实验运行结果参考程序见下 :#include stdio.h #include string.h #include conio.h #include stdlib.h #define MAXNAME 25 /*the largest length of mfdname,ufdname,filename*/ #define MAXCHILD 50 /*the la
3、rgest child*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 20 页 - - - - - - - - - 2 #define MAX (MAXCHILD*MAXCHILD) /*the size of fpaddrno*/ typedef struct /*the structure of OSFILE*/ int fpaddr; /*file physical address*/ int flength; /*file length*/ int fmod
4、e; /*file mode:0-Read Only;1-Write Only;2-Read and Write(default);*/ char fnameMAXNAME; /*file name*/ OSFILE; typedef struct /*the structure of OSUFD*/ char ufdnameMAXNAME; /*ufd name*/ OSFILE ufdfileMAXCHILD; /*ufd own file*/ OSUFD; typedef struct /*the structure of OSUFDLOGIN*/ char ufdnameMAXNAME
5、; /*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-write only,2-read and write,3-initial*/ OSUFD_OPENMODE; OSUFD *ufdMAXCHILD; /*ufd and ufd own files*/ OSUFD_LOGIN ufd_lp; int ucount=0; /
6、*the count of mfds ufds*/ int fcountMAXCHILD; /*the count of ufds files*/ int loginsuc=0; /*whether login successfully*/ char usernameMAXNAME; /*record login users name22*/ char dirnameMAXNAME;/*record current directory*/ int fpaddrnoMAX; /*record file physical address num*/ OSUFD_OPENMODE ifopenMAX
7、CHILDMAXCHILD; /*record file open/close*/ int wgetchar; /*whether getchar()*/ FILE *fp_mfd,*fp_ufd,*fp_file_p,*fp_file; void main() int i,j,choice1; char choice50; /*choice operation:dir,create,delete,open,delete,modify,read,write*/ int choiceend=1; /*whether choice end*/ char *rtrim(char *str); /*r
8、emove the trailing blanks.*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 20 页 - - - - - - - - - 3 char *ltrim(char *str); /*remove the heading blanks.*/ void LoginF(); /*LOGIN FileSystem*/ void DirF(); /*Dir FileSystem*/ void CdF(); /*Change Dir*/ void Creat
9、eF(); /*Create File*/ void DeleteF(); /*Delete File*/ void ModifyFM(); /*Modify FileMode*/ void OpenF(); /*Open File*/ void CloseF(); /*Close File*/ void ReadF(); /*Read File*/ void WriteF(); /*Write File*/ void QuitF(); /*Quit FileSystem*/ void help(); if(fp_mfd=fopen(c:osfilemfd,rb)=NULL) fp_mfd=f
10、open(c:osfilemfd,wb); fclose(fp_mfd); for(i=0;i,strupr(dirname); else printf(Bad command or file name.nC:%s,strupr(username); gets(choice); strcpy(choice,ltrim(rtrim(strlwr(choice); if (strcmp(choice,dir)=0) choice1=1; else if(strcmp(choice,creat)=0) choice1=2; else if(strcmp(choice,delete)=0) choic
11、e1=3; else if(strcmp(choice,attrib)=0) choice1=4; else if(strcmp(choice,open)=0) choice1=5; else if(strcmp(choice,close)=0) choice1=6; else if(strcmp(choice,read)=0) choice1=7; else if(strcmp(choice,modify)=0) choice1=8; else if(strcmp(choice,exit)=0) choice1=9; else if(strcmp(choice,cls)=0) choice1
12、=10; else if(strcmp(choice,cd)=0) choice1=11; else if(strcmp(choice,help)=0) choice1=20; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 20 页 - - - - - - - - - 4 else choice1=12; switch(choice1) case 1:DirF();choiceend=1;break; case 2:CreateF();choiceend=1;if(!w
13、getchar) getchar();break; case 3:DeleteF();choiceend=1;if(!wgetchar)getchar();break; case 4:ModifyFM();choiceend=1;if(!wgetchar) getchar();break; case 5:choiceend=1;OpenF();if (!wgetchar) getchar();break; case 6:choiceend=1;CloseF();if (!wgetchar) getchar();break; case 7:choiceend=1;ReadF();if (!wge
14、tchar) getchar();break; case 8:choiceend=1;WriteF();if (!wgetchar) getchar();break; case 9:printf(nYou have exited this system.); QuitF();exit(0);break; case 10:choiceend=1;clrscr();break; case 11:CdF();choiceend=1;break; case 20:help();choiceend=1;break; default:choiceend=0; else printf(nAccess den
15、ied.); void help(void) printf(nThe Command Listn); printf(nCd Attrib Creat Modify Read Open Cls Delete Exit Closen); char *rtrim(char *str) /*remove the trailing blanks.*/ int n=strlen(str)-1; while(n=0) if(*(str+n)!= ) *(str+n+1)=0; break; else n-; if (nufdname,strupr(ufd_lp.ufdname); fp_ufd=fopen(
16、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(fp_ufd); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 20 页 - - - - - - - - - 6 fclose(fp_mfd); ucount=j; SetPANo(0); print
17、f(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),N)=0)loginsuc=0;flag=0;return; else pri
18、ntf(New Password(=8):); InputPW(loginpw); /*input new password,use * replace*/ printf(nConfirm Password(ufdname,strupr(ufd_lp.ufdname); fp_ufd=fopen(str,rb); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 20 页 - - - - - - - - - 7 for(i=0;fread(&ufdj-ufdfilei,si
19、zeof(OSFILE),1,fp_ufd)!=0;i+,fcountj+) ifopenji.ifopen=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):); g
20、ets(a); ltrim(rtrim(a); if (strcmp(strupr(a),Y)=0) loginsuc=0;flag=0; else if(strcmp(strupr(a),N)=0)loginsuc=0;flag=0;return; void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/ int i,j; if (RorW=0) if(fp_file_p=fopen(c:osfilefilefile_p,rb)=NULL) fp_file_p=fopen(c:osfilefilefile_p,wb)
21、; fclose(fp_file_p); fp_file_p=fopen(c:osfilefilefile_p,rb); for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i+) fpaddrnoj=1; /*for(i=1;iMAX;i+) if (i%13)=0) fpaddrnoi=1;*/ else fp_file_p=fopen(c:osfilefilefile_p,wb); /*for(i=1;iMAX;i+) if(i%13)=0) fpaddrnoi=0;*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - -
22、- - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 20 页 - - - - - - - - - 8 for(i=0;iMAX;i+) if (fpaddrnoi=1) fwrite(&i,sizeof(int),1,fp_file_p); fclose(fp_file_p); void InputPW(char *password) /*input password,use * replace*/ int j; for(j=0;j0) j-;j-; putchar(b);putchar( );putchar(b); else j-; else pas
23、swordj=0; break; passwordj=0; void DirF() /*Dir FileSystem*/ int i,j,count=0; char sfmode25,sfpaddr25,str25; int ExistD(char *dirname); /*Whether DirName Exist,Exist-i,Not Exist-0*/ clrscr(); if (strcmp(strupr(ltrim(rtrim(dirname),)!=0) printf(nnC:%sdirn,dirname); printf(n%14s%16s%14s%10s%18sn,FileN
24、ame,FileAddress,FileLength,Type,FileMode); j=ExistD(dirname); for(i=0;iufdfilei.fpaddr,str,10); 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 A
25、nd Write); else strcpy(sfmode,Protect); printf(%14s%16s%14d%10s%18sn,ufdj-ufdfilei.fname,sfpaddr,ufdj-ufdfilei.flength,sfmode); printf(n %3d file(s)n,fcountj); else printf(nnC:dirn); printf(n%14s%18s%8sn,DirName,OwnFileCount,Type); for(i=0;iufdname,fcounti,); count=count+fcounti; printf(n %3d dir(s)
26、,%5d file(s)n,ucount,count); int ExistD(char *dirname) /*Whether DirName Exist,Exist-i,Not Exist-0*/ int i; int exist=0; for(i=0;iufdname),strupr(dirname)=0) exist=1; break; if (exist) return(i); else return(-1); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 9 页,共 2
27、0 页 - - - - - - - - - 10 void CdF() /*Exchange Dir*/ char dnameMAXNAME; char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the heading blanks.*/ int ExistD(char *filename); /*Whether FileName Exist,Exist-i,Not Exist-0*/ printf(nPlease input DirName (cd.-Previous
28、 dir; DirNAME-cd DirNAME):); gets(dname); ltrim(rtrim(dname); if (ExistD(dname)=0) strcpy(dirname,strupr(dname); else if(strcmp(strupr(dname),CD.)=0) strcpy(ltrim(rtrim(dirname),); else printf(nError.%s does not exist.n,dname); void CreateF() /*Create File*/ int fpaddrno,flag=1,i; char fnameMAXNAME,
29、str50,str150,strtext255,a25; char fmode25; char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the heading blanks.*/ int FindPANo(); /*find out physical address num*/ int WriteF1(); /*write file*/ int ExistF(char *filename); /*Whether FileName Exist,Exist-i,Not E
30、xist-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); wget
31、char=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) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 10 页
32、,共 20 页 - - - - - - - - - 11 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,s
33、tr1,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)
34、,N)=0)flag=0;wgetchar=1; printf(n%s has been created successfully!n,fname); else printf(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;
35、 int ExistD(char *dirname); j=ExistD(dirname); for(i=0;iufdfilei.fname),strupr(filename)=0) exist=1; break; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 11 页,共 20 页 - - - - - - - - - 12 if (exist) return(i); else return(-1); int FindPANo() /*find out physical addre
36、ss num*/ int i; for(i=0;iMAX;i+) if (fpaddrnoi=0) fpaddrnoi=1;break; if (i=0) k=ExistD(username); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 12 页,共 20 页 - - - - - - - - - 13 if(ifopenki.ifopen=1) printf(nError. %s is in open status. Close it before delete.n,fname
37、);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
38、-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
39、hasnt been deleted.n,fname); wgetchar=1; flag=0; else printf(nError. %s does not exist.n,fname);wgetchar=1; void ModifyFM() /*Modify FileMode*/ char fnameMAXNAME,str50; int i,j,k,flag; char fmode25; /*whether delete*/ char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*
40、remove the heading blanks.*/ void InputPW(char *password); /*input password,use * replace*/ void SetPANo(int RorW); /*Set physical address num*/ 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 13 页,共 20 页 - - - - - - - - - 14 int ExistF(char *filename); /*Whether File
41、Name Exist,Exist-i,Not Exist-0*/ int ExistD(char *dirname); if (strcmp(strupr(dirname),strupr(username)!=0) printf(nError.You can only modify filemode in yourself dir.n);wgetchar=1; else printf(nPlease input FileName:); gets(fname); ltrim(rtrim(fname); i=ExistF(fname); if (i=0) k=ExistD(username); i
42、f(ifopenki.ifopen=1) printf(nError.%s is in open status. Close it before modify.n,fname);wgetchar=1; else if(ufdk-ufdfilei.fmode=0) strcpy(str,read only); /*FileMode*/ else if(ufdk-ufdfilei.fmode=1) strcpy(str,write only); else if(ufdk-ufdfilei.fmode=2) strcpy(str,read and write); else strcpy(str,Pr
43、otect); 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=0; printf(n%s has been modified to READ ONLY mode successfully.n,fname); wgetchar=1; else if(strcm
44、p(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 WRITE mode successfully.n,fname); wgetchar=1; else if(strcmp(fmode,3)=0) ufdk-ufdfilei.fm
45、ode=3; printf(n%s has been modified to FORBID mode successfully.n,fname); wgetchar=1; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 14 页,共 20 页 - - - - - - - - - 15 else printf(nError.%s is not modified.n,fname);wgetchar=1; else printf(nError. %s dose not exist.n,fn
46、ame);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
47、 *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(nErr
48、or. 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
49、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*/ 名师资料总结 - - -精品资料欢迎下载
50、- - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 15 页,共 20 页 - - - - - - - - - 16 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-ufd