操作系统课程设计汇本二级文件系统_第1页
操作系统课程设计汇本二级文件系统_第2页
操作系统课程设计汇本二级文件系统_第3页
操作系统课程设计汇本二级文件系统_第4页
操作系统课程设计汇本二级文件系统_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

...wd......wd......wd...操作系统课程设计报告操作系统课程设计报告专业:计算机信息处理学号:姓名:提交日期:【设计目的】1.课程设计目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能和内部实现。2.结合数据构造、程序设计、计算机原理等课程的知识,设计一个二级文件系统,进一步理解操作系统。3.通过对实际问题的分析、设计、编程实现,提高学生实际应用、编程的能力【设计内容】1、delete删除文件2、open翻开文件3、close关闭文件4、write写文件【实验环境】Windows7系统Visualstudio2010【相关知识综述】本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户帐号下的文件。另外,为了简便文件系统未考虑文件共享,文件系统安全以及管道文件与设备文件等特殊内容。首先应确定文件系统的数据构造:主目录、子目录及活动文件等。主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改。用户创立的文件,可以编号存储于磁盘上。如:file0,file1,file2…并以编号作为物理地址,在目录中进展登记。【设计思路】1主要数据构造#defineMAXNAME25/*thelargestlengthofmfdname,ufdname,filename*/#defineMAXCHILD50/*thelargestchild每个用户名下最多有50个文件*/#defineMAX(MAXCHILD*MAXCHILD)/*thesizeoffpaddrno*/typedefstruct/*thestructureofOSFILE定义主文件*/{intfpaddr;/*filephysicaladdress*/intflength;/*filelength*/intfmode;/*filemode:0-ReadOnly;1-WriteOnly;2-ReadandWrite;3-Protect;*/charfname[MAXNAME];/*filename*/}OSFILE;typedefstruct/*thestructureofOSUFD定义用户文件目录*/{charufdname[MAXNAME];/*ufdname*/ OSFILEufdfile[MAXCHILD];/*ufdownfile*/}OSUFD;typedefstruct/*thestructureofOSUFD'LOGIN定义登陆*/{charufdname[MAXNAME];/*ufdname*/charufdpword[8];/*ufdpassword*/}OSUFD_LOGIN;typedefstruct/*fileopenmode定义操作方式*/{intifopen;/*ifopen:0-close,1-open*/intopenmode;/*0-readonly,1-writeonly,2-readandwrite,3-initial*/}OSUFD_OPENMODE;2主要函数voidLoginF();/*LOGINFileSystem*/voidDirF();/*DirFileSystem*/voidCreateF();/*CreateFile*/voidDeleteF();/*DeleteFile*/voidModifyFM();/*ModifyFileMode*/voidOpenF();/*OpenFile*/voidCloseF();/*CloseFile*/voidReadF();/*ReadFile*/voidWriteF();/*WriteFile*/voidQuitF();/*QuitFileSystem*/voidCdF();/*ChangeDir*/voidhelp();【主要程序段】1Delete函数voidDeleteF()/*DeleteFile*/{charfname[MAXNAME],str[50],str1[50]; inti,k,j; intfpaddrno1; if(strcmp(strupr(ltrim(rtrim(dirname))),"")==0)/*无法删除主目录的文件*/ { printf("\nError.Pleaseconverttoufddirbeforedelete.\n"); wgetchar=1; } if(strcmp(strupr(dirname),strupr(username))!=0)/*无法删除非自己目录的文件*/ { printf("\nError.Youcanonlymodifyfilemodeinyourselfdir.\n"); wgetchar=1; } else { printf("\nPleaseinputFileName:"); gets(fname);//从键盘获取所要删除的文件名 ltrim(rtrim(fname)); i=ExistF(fname);//获取文件编号 if(i>=0) { k=ExistD(username); if(ifopen[k][i].ifopen==1)/*文件翻开时无法删除*/ { printf("\nError.\'%s\'isinopenstatus.Closeitbeforedelete.\n",fname); wgetchar=1; } else { if(ufd[k]->ufdfile[i].fmode==3)/*被保护的文件无法删除*/ { printf("\nError.\'%s\'isinprotectstatus.Closeitbeforedelete.\n",fname); wgetchar=1; } else { fpaddrno1=ufd[k]->ufdfile[i].fpaddr;//获取文件对应的物理文件名 fpaddrno[fpaddrno1]=0;//回收盘块 for(j=i;j<fcount[k];j++) { ufd[k]->ufdfile[j]=ufd[k]->ufdfile[j+1];//从被删除的文件号开场,数组值全部前移一个 } strcpy(str,"d:\\osfile\\file\\file"); itoa(fpaddrno1,str1,10);//整数转化成字符串 strcat(str,str1); strcat(str,".txt"); remove(str);//删除物理文件 fcount[k]--;//k用户文件数量少1 printf("\n\'%s\'isdeletedsuccessfully.\n",fname); wgetchar=1; } } } else//所要删除的文件不存在 { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}2Open函数voidOpenF()/*OpenFile*/{ charfname[MAXNAME]; inti,k,j; if(strcmp(strupr(dirname),strupr(username))!=0)/*在自己的目录里才能翻开*/ { printf("\nError.Youcanonlyopeninyourselfdir.\n"); wgetchar=1; } else { k=ExistD(username); for(j=0;j<fcount[k];j++) { printf("%15s",ufd[k]->ufdfile[j].fname); }printf("\nPleaseinputFileName:"); gets(fname);//获取所要翻开的文件名 ltrim(rtrim(fname)); i=ExistF(fname);//获取目录编号 if(i>=0) { if(ifopen[k][i].ifopen==1)//输入的文件是翻开的 { printf("\nError.\'%s\'isinopenstatus.\n",fname); wgetchar=1; } else { ifopen[k][i].ifopen=1;//修改为翻开的 if(ufd[k]->ufdfile[i].fmode==0)/*根据文件的模式设置翻开模式*/ {ifopen[k][i].openmode=0;} elseif(ufd[k]->ufdfile[i].fmode==1) {ifopen[k][i].openmode=1;} elseif(ufd[k]->ufdfile[i].fmode==2) {ifopen[k][i].openmode=2;} elseifopen[k][i].openmode=3;printf("\n\'%s\'isopenedsuccessfully\n",fname); wgetchar=1; } } else//文件不存在 { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}3Close函数voidCloseF()/*CloseFile*/{ charfname[MAXNAME]; inti,k,j; if(strcmp(strupr(dirname),strupr(username))!=0)/*不在自己的目录里没法进展*/ { printf("\nError.Youcanonlyclosefileinyourselfdir.\n"); wgetchar=1; } else { k=ExistD(username); for(j=0;j<fcount[k];j++)/*列出已经翻开的文件*/ { if(ifopen[k][j].ifopen==1)//如果文件是翻开的 printf("%15s",ufd[k]->ufdfile[j].fname); }printf("\nPleaseinputFileName:"); gets(fname);//从键盘上读入所要关闭的文件 ltrim(rtrim(fname)); i=ExistF(fname);//获取文件编号 if(i>=0) { ifopen[k][i].ifopen=0;/*关闭文件*/ printf("\n\'%s\'closedsuccessfully\n",fname); wgetchar=1; } else//所要关闭的文件不存在 { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}4Write函数voidWriteF()/*WriteFile*/{ inti,k,n=0; intlength; charfname[MAXNAME],values[1000]; charstr[255],str1[255],c; if(strcmp(strupr(ltrim(rtrim(dirname))),"")==0)//只能写自己目录下的文件 { printf("\nError.Pleaseconverttoufddirbeforewrite.\n"); wgetchar=1; return; } printf("\nCaution:Openfilefirst\n"); printf("OpenedFile(s)List:\n"); k=ExistD(dirname);//获取目录编号 for(i=0;i<fcount[k];i++)/*列出可以写的文件*/ { if(ifopen[k][i].ifopen==1)//如果文件是翻开的 { printf("%15s",ufd[k]->ufdfile[i].fname); n++; } if((n%4==0)&&(n!=0))printf("\n"); } printf("\n%dfilesopenned.\n",n); if(n==0)wgetchar=1;//没有翻开的文件 if(n!=0) { printf("\nPleaseinputFileName:"); gets(fname);//从键盘获取所要写的文件 ltrim(rtrim(fname)); i=ExistF(fname);//获取文件编号 if(i>=0) { if(ifopen[k][i].ifopen==1)//如果文件是翻开的 { if((ifopen[k][i].openmode==1)||(ifopen[k][i].openmode==2))//文件是只写,或者是可读可写的 { itoa(ufd[k]->ufdfile[i].fpaddr,str,10);//获取文件对应的物理地址 strcpy(str1,"file"); strcat(str1,str); strcpy(str,"d:\\osfile\\file\\"); strcat(str,str1); strcat(str,".txt"); fp_file=fopen(str,"ab");//以二进制只写的形式翻开,每次将内容添加到文件末尾接着上一次内容 length=WriteF1(); ufd[k]->ufdfile[i].flength=ufd[k]->ufdfile[i].flength+length;//计算总长度 printf("\n\n%dLength.\n",ufd[k]->ufdfile[i].flength); printf("\n\nYouhavewritefilesuccessfully!!"); fclose(fp_file); wgetchar=0; } elseif(ifopen[k][i].openmode==0)//文件是只读的 { printf("\nError.\'%s\'hasbeenopenedwithREADONLYmode.Itisn\'twrite.\n",fname); wgetchar=1; } else//文件是保护的 { printf("\nError.\'%s\'hasbeenopenedwithPROTECTmode.Itisn\'twrite.\n",fname); wgetchar=1; } } else//文件是关闭的 { printf("\nError.\'%s\'isinclosingstatus.Pleaseopenitbeforewrite\n",fname); wgetchar=1; } } else//所指定的文件不存在 { printf("\nError.\'%s\'doesnotexist.\n",fname); wgetchar=1; } }}【程序流程设计】总的功能构造图:局部子模块程序流程图〔1〕翻开命令的程序流程图:〔2〕关闭命令的程序流程图:〔3〕写命令的程序流程图:〔4〕删除命令的程序流程图:【测试结果】1删除文件2翻开的文件不能删除3翻开文件,其中已经翻开的文件不能再次翻开3关闭文件4写文件,其中只有翻开了文件才能写入5写文件6只读文件和保护文件不能写入7其他函数【参考文献】计算机操作系统,西安电子科技大学出版社,方敏主编,2004.8局部函数含义引用于msdn.microsoft【源程序清单】#include"stdio.h"#include"string.h"#include"conio.h"#include"stdlib.h"#defineMAXNAME25/*thelargestlengthofmfdname,ufdname,filename*/#defineMAXCHILD50/*thelargestchild每个用户名下最多有50个文件*/#defineMAX(MAXCHILD*MAXCHILD)/*thesizeoffpaddrno*/typedefstruct/*thestructureofOSFILE定义主文件*/{intfpaddr;/*filephysicaladdress*/intflength;/*filelength*/intfmode;/*filemode:0-ReadOnly;1-WriteOnly;2-ReadandWrite;3-Protect;*/charfname[MAXNAME];/*filename*/}OSFILE;typedefstruct/*thestructureofOSUFD定义用户文件目录*/{charufdname[MAXNAME];/*ufdname*/ OSFILEufdfile[MAXCHILD];/*ufdownfile*/}OSUFD;typedefstruct/*thestructureofOSUFD'LOGIN定义登陆*/{charufdname[MAXNAME];/*ufdname*/charufdpword[8];/*ufdpassword*/}OSUFD_LOGIN;typedefstruct/*fileopenmode定义操作方式*/{intifopen;/*ifopen:0-close,1-open*/intopenmode;/*0-readonly,1-writeonly,2-readandwrite,3-protect*/}OSUFD_OPENMODE;voidLoginF();/*LOGINFileSystem*/voidDirF();/*DirFileSystem*/voidCreateF();/*CreateFile*/voidDeleteF();/*DeleteFile*/voidModifyFM();/*ModifyFileMode*/voidOpenF();/*OpenFile*/voidCloseF();/*CloseFile*/voidReadF();/*ReadFile*/voidWriteF();/*WriteFile*/voidQuitF();/*QuitFileSystem*/voidCdF();/*ChangeDir*/voidhelp();char*rtrim(char*str);/*removethetrailingblanks.*/char*ltrim(char*str);/*removetheheadingblanks.*/voidInputPW(char*password);/*inputpassword,use'*'replace*/intExistD(char*dirname);/*WhetherDirNameExist,Exist-i,NotExist-0*/intWriteF1();/*writefile*/intExistF(char*filename);/*WhetherFileNameExist,Exist-i,NotExist-0*/voidSetPANo(intRorW);/*Setphysicaladdressnum*/intFindPANo();/*findoutphysicaladdressnum*/intucount=0;/*thecountofmfd'sufds用户数*/intfcount[MAXCHILD];/*thecountofufd'sfiles子文件数*/intloginsuc=0;/*whetherloginsuccessfully登陆成功*/charusername[MAXNAME];/*recordloginuser'sname22用户名*/chardirname[MAXNAME];/*recordcurrentdirectory使用的用户目录*/intfpaddrno[MAX];/*recordfilephysicaladdressnum物?理地址号,存放自己所创立的所有文件的地址*/intwgetchar;/*whethergetchar()*/OSUFD*ufd[MAXCHILD];/*ufdandufdownfiles*/OSUFD_LOGINufd_lp;//用户登录构造体类型的变量OSUFD_OPENMODEifopen[MAXCHILD][MAXCHILD];/*recordfileopen/close*/FILE*fp_mfd,*fp_ufd,*fp_file_p,*fp_file;//FILE是变量类型,实际上是语言定义的标准数据构造,用于文件voidclrscr()/*清屏*/{ system("cls");}voidmain(){inti,choice1;charchoice[50];/*choiceoperation:dir,create,delete,open,delete,modify,read,write*/intchoiceend=1;/*whetherchoiceend*///fopen标准函数,翻开文件msd.txt,翻开一个二进制文件,只允许读数据,送返指针,指向FILE类型对象。if((fp_mfd=fopen("d:\\osfile\\mfd.txt","rb"))==NULL) { fp_mfd=fopen("d:\\osfile\\mfd.txt","wb");//翻开一个二进制文件,只允许写数据 fclose(fp_mfd);//关闭一个流 }for(i=0;i<MAX;i++)fpaddrno[i]=0;clrscr();/*clearscreen*/ LoginF();/*userlogin*/if(loginsuc==1)/*LoginSuccessfully*/ {while(1) { wgetchar=0;if(choiceend==1) printf("\n\nd:\\%s>",strupr(dirname));//表示以字符串型输出else printf("Badcommandorfilename.Choosehelp\nd:\\%s>",strupr(dirname));//strupr-变为大写字母 gets(choice); strcpy(choice,ltrim(rtrim(strlwr(choice))));//strlwr将字符串转换为小写形式,strcpy(dest,char)复制//ltrim(rtrim())去掉输入用户名的头部和尾部空格if(strcmp(choice,"dir")==0)choice1=1;//strcmp一样为0elseif(strcmp(choice,"create")==0)choice1=2;elseif(strcmp(choice,"delete")==0)choice1=3;elseif(strcmp(choice,"attrib")==0)choice1=4;elseif(strcmp(choice,"open")==0)choice1=5;elseif(strcmp(choice,"close")==0)choice1=6;elseif(strcmp(choice,"read")==0)choice1=7;elseif(strcmp(choice,"write")==0)choice1=8;elseif(strcmp(choice,"exit")==0)choice1=9;elseif(strcmp(choice,"cls")==0)choice1=10;elseif(strcmp(choice,"cd")==0)choice1=11;elseif(strcmp(choice,"help")==0)choice1=20;elsechoice1=12;switch(choice1) {case1:DirF();choiceend=1;break;case2:CreateF();choiceend=1;if(!wgetchar)getchar();break;case3:DeleteF();choiceend=1;if(!wgetchar)getchar();break;case4:ModifyFM();choiceend=1;if(!wgetchar)getchar();break;case5:OpenF();choiceend=1;if(!wgetchar)getchar();break;case6:CloseF();choiceend=1;if(!wgetchar)getchar();break;case7:ReadF();choiceend=1;if(!wgetchar)getchar();break;case8:WriteF();choiceend=1;if(!wgetchar)getchar();break;case9:printf("\nYouhaveexitedthissystem."); QuitF();exit(0);break;case10:clrscr();choiceend=1;break;case11:CdF();choiceend=1;break;//更改目录名称case20:help();choiceend=1;break;default:choiceend=0; } } }else printf("\nAccessdenied.");}char*rtrim(char*str)/*removethetrailingblanks.去掉登陆用户名的尾空格*/{intn=strlen(str)-1;while(n>=0) {if(*(str+n)!='') { *(str+n+1)='\0';break; }elsen--; }if(n<0)str[0]='\0';returnstr;}char*ltrim(char*str)/*removetheheadingblanks.去掉登陆用户名的头空格*/{ strrev(str);//字符的顺序颠倒 rtrim(str); strrev(str);returnstr;}voidSetPANo(intRorW)/*Setphysicaladdressnum,0-read,1-write物理地址*/{inti,j;if(RorW==0) {if((fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","rb"))==NULL)//只读 { fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","wb");//只写 fclose(fp_file_p); } fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","rb");//只读//fread读数据(输入地址,大小,个数,提供数据的文件),sizeof是内存空间的大小不是长度for(i=0;fread(&j,sizeof(int),1,fp_file_p)!=0;i++) fpaddrno[j]=1; }else { fp_file_p=fopen("d:\\osfile\\file\\file_p.txt","wb");//只写for(i=0;i<MAX;i++)if(fpaddrno[i]==1) fwrite(&i,sizeof(int),1,fp_file_p);//fwrite写数据输出地址,字节数,个数,目标文件) } fclose(fp_file_p);}voidInputPW(char*password)/*inputpassword,use'*'replace*/{intj;for(j=0;j<=7;j++) { password[j]=getch();//gerch()返回从键盘上读取到的字符if((int)(password[j])!=13) {if((int)(password[j])!=8) putchar('*');//输出一个字符else {if(j>0) { j--; putchar('\b');putchar('');putchar('\b'); }elsej--; } }else { password[j]='\0';break; } } password[j]='\0';}intExistD(char*dirname)/*WhetherDirNameExist,Exist-i,NotExist-(-1)*/{inti;intexist=0;for(i=0;i<ucount;i++)if(strcmp(strupr(ufd[i]->ufdname),strupr(dirname))==0) { exist=1;break; }if(exist)return(i);elsereturn(-1);}intExistF(char*filename)/*WhetherFileNameExist,Exist-i,NotExist-(-1)*/{inti,j;intexist=0; j=ExistD(dirname);for(i=0;i<fcount[j];i++)if(strcmp(strupr(ufd[j]->ufdfile[i].fname),strupr(filename))==0) { exist=1;break; }if(exist)return(i);elsereturn(-1);}intFindPANo()/*findoutphysicaladdressnum*/{inti;for(i=0;i<MAX;i++)if(fpaddrno[i]==0) { fpaddrno[i]=1;break; }if(i<MAX)return(i);elsereturn(-1);}intWriteF1()/*writefile*/{intlength=0;charc; printf("Pleaseinputtext(\'#\'standsforend):\n");while((c=getchar())!='#') { fprintf(fp_file,"%c",c);if(c!='\n')length++; } fprintf(fp_file,"\n"); fclose(fp_file);return(length);}voidLoginF()/*LOGINFileSystem登陆函数*/{charloginame[MAXNAME],loginpw[9],logincpw[9],str[50];//loginpw-输入密码,logincpw-确认输入密码,str[50]存放地址inti,j,flag=1;chara[25];intfindout;/*loginusernotexist*/while(1) { findout=0; printf("\n\nLoginName:"); gets(loginame);//把用户名写入系统 ltrim(rtrim(loginame)); fp_mfd=fopen("d:\\osfile\\mfd.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)/*userexist*///该用户存在 { printf("LoginPassword:");//用户密码输入 InputPW(loginpw);/*inputpassword,use'*'replace*/if(strcmp(loginpw,logincpw)==0)//相等 { strcpy(username,strupr(loginame)); strcpy(dirname,username); fp_mfd=fopen("d:\\osfile\\mfd.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到dest结尾处 strcat(str,".txt"); ufd[j]=(OSUFD*)malloc(sizeof(OSUFD));//malloc向系统申请分配指定size个字节的内存空间分配二级文件空间 strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname)); fp_ufd=fopen(str,"rb"); fcount[j]=0;//当前用户文件数目for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++)//记录用户文件 { ifopen[j][i].ifopen=0; ifopen[j][i].openmode=4; } fclose(fp_ufd); } fclose(fp_mfd); ucount=j; SetPANo(0); printf("\n\nLoginsuccessful!WelcometothisFileSystem\n\n"); loginsuc=1;return; }else//不相等 { printf("\n\n"); flag=1;while(flag) { printf("LoginFailed!PasswordError.TryAgain(Y/N):"); gets(a); ltrim(rtrim(a));if(strcmp(strupr(a),"Y")==0) { loginsuc=0; flag=0; }elseif(strcmp(strupr(a),"N")==0) { loginsuc=0; flag=0;return; } } } }else/*usernotexist*/ { printf("NewPassword(<=8):"); InputPW(loginpw);/*inputnewpassword,use'*'replace*/ printf("\nConfirmPassword(<=8):");/*inputnewpassword,use'*'replace*/ InputPW(logincpw);if(strcmp(loginpw,logincpw)==0)//相等 { strcpy(ufd_lp.ufdname,strupr(loginame));//写入目录 strcpy(ufd_lp.ufdpword,loginpw); fp_mfd=fopen("d:\\osfile\\mfd.txt","ab"); fwrite(&ufd_lp,sizeof(OSUFD_LOGIN),1,fp_mfd);//写入文件 fclose(fp_mfd); strcpy(username,strupr(loginame)); strcpy(dirname,loginame); strcpy(str,"d:\\osfile\\");//写入地址 strcat(str,username); strcat(str,".txt");if((fp_ufd=fopen(str,"rb"))==NULL) { fp_ufd=fopen(str,"wb"); fclose(fp_ufd); } fp_mfd=fopen("d:\\osfile\\mfd.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(str,".txt"); ufd[j]=(OSUFD*)malloc(sizeof(OSUFD)); strcpy(ufd[j]->ufdname,strupr(ufd_lp.ufdname)); fp_ufd=fopen(str,"rb");for(i=0;fread(&ufd[j]->ufdfile[i],sizeof(OSFILE),1,fp_ufd)!=0;i++,fcount[j]++)//记录用户文件 { ifopen[j][i].ifopen=0; ifopen[j][i].openmode=4; } fclose(fp_ufd); } fclose(fp_mfd); ucount=j; SetPANo(0); printf("\n\nLoginSuccessful!WelcometothisSystem\n\n"); loginsuc=1;return; }else//不相等 { printf("\n\n"); flag=1;while(flag) { printf("LoginFailed!PasswordError.TryAgain(Y/N):"); gets(a); ltrim(rtrim(a));if(strcmp(strupr(a),"Y")==0) { loginsuc=0; flag=0; }elseif(strcmp(strupr(a),"N")==0) { loginsuc=0; flag=0;return; } } } } }}voidDirF()/*DirFileSystem主目录*/{inti,j,count=0;charsfmode[25],sfpaddr[25],str[25]; clrscr();if(strcmp(strupr(ltrim(rtrim(dirname))),"")!=0)//不为空 { printf("\n\nd:\\%s>dir\n",dirname); printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","FileMode"); j=ExistD(dirname);for(i=0;i<fcount[j];i++) {if((i%16==0)&&(i!=0))//限制长度 { printf("\nPressanykeytocontinue.."); getch(); clrscr(); printf("\n%14s%16s%14s%10s%18s\n","FileName","FileAddress","FileLength","Type","FileMode"); } itoa(ufd[j]->ufdfile[i].fpaddr,str,10);//itoa()把整数转换为字符串 strcpy(sfpaddr,"file"); strcat(sfpaddr,str);if(ufd[j]->ufdfile[i].fmode==0)strcpy(sfmode,"ReadOnly");elseif(ufd[j]->ufdfile[i].fmode==1)strcpy(sfmode,"WriteOnly");elseif(ufd[j]->ufdfile[i].fmode==2)strcpy(sfmode,"ReadAndWrite");elsestrcpy(sfmode,"Protect"); printf("%14s%16s%14d%10s%18s\n",ufd[j]->ufdfile[i].fname,sfpaddr,ufd[j]->ufdfile[i].flength,"<FILE>",sfmode); } printf("\n%3dfile(s)\n",fcount[j]); }else//空 { printf("\n\nd:\\>dir\n"); printf("\n%14s%18s%8s\n","DirName","OwnFileCount","Type");for(i=0;i<ucount;i++) {if((i%16==0)&&(i!=0)) { printf("\nPressanykeytocontinue..."); getch(); clrscr(); printf("\n%14s%18s%8s\n","DirName","OwnFileCount","Type"); } printf("%14s%18d%8s\n",ufd[i]->ufdname,fcount[i],"<UFD>"); count=count+fcount[i]; } printf("\n%3ddir(s),%5dfile(s)\n",ucount,count); }}voidCreateF()/*CreateFile*/{intfpaddrno,flag=1,i;charfname[MAXNAME],str[50],str1[50],a[25];charfmode[25];if(strcmp(strupr(dirname),strupr(username))!=0)//目录名与用户名不相等 { printf("\nError.Youmustcreatefileinyourowndir.\n"); wgetchar=1; }else { printf("\nPleaseinputFileName:"); gets(fname); ltrim(rtrim(fname));if(ExistF(fname)>=0) { printf("\nError.Name\'%s\'hasalreadyexisted.\n",fname); wgetchar=1; }else { printf("PleaseinputFileMode(0-ReadOnly,1-WriteOnly,2-ReadandWrite,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(ufd[i]->ufdfile[fcount[i]].fname,fname);//记录 ufd[i]->ufdfile[fcount[i]].fpaddr=fpaddrno; ufd[i]->ufdfile[fcount[i]].fmode=atoi(fmode); ifopen[i][fcount[i]].ifopen=0; ifopen[i][fcount[i]].openmode=4; strcpy(str,"d:\\osfile\\file\\file"); itoa(fpaddrno,str1,10); strcat(str,str1); strcat(str,".txt"); fp_file=fopen(str,"wb"); fclose(fp_file); fcount[i]++;while(flag) { printf("Inputtextnow(Y/N):"); gets(a); ltrim(rtrim(a)); ufd[i]->ufdfile[fcount[i]-1].flength=0;if(strcmp(strupr(a),"Y")==0) { fp_file=fopen(str,"wb+");//写入二进制文件 ufd[i]->ufdfile[fcount[i]-1].flength=WriteF1(); flag=0; }elseif(strcmp(strupr(a),"N")==0) { flag=0; wgetchar=1; } } printf("\n\'%s\'hasbeencreatedsuccessfully!\n",fname); }else { printf("\nFail!NoDiskSpace.Pleaseformatyourdisk.\n"); wgetchar=1; } }else { printf("\nError.FileMode\'sRangeis0-3\n"); wgetchar=1; } } }}voidDeleteF()/*DeleteFile*/{charfname[MAXNAME],str[50],str1[50];inti,k,j;intfpaddrno1;if(strcmp(strupr(ltrim(rtrim(dirname))),"")==0)/*无法删除主目录的文件*/ { printf("\nError.Pleaseconverttoufddirbeforedelete.\n"); wgetchar=1; }if(strcmp(strupr(dirname),strupr(username))!=0)/*无法删除非自己目录的文件*/ { printf("\nError.Youcanonlymodifyfilemodeinyourselfdir.\n"); wgetchar=1; }else { printf("\nPleaseinputFileName:"); gets(fname);//从键盘获取所要删除的文件名 ltrim(rtrim(fname)); i=ExistF(fname);//获取文件编号if(i>=0) { k=ExistD(username);if(ifopen[k][i].ifopen==1)/*文件翻开时无法删除*/ { printf("\nError.\'%s\'isinopenstatus.Closeitbeforedelete.\n",fname); wgetchar=1; }else {if(ufd[k]->ufdfile[i].fmode==3)/*被保护的文件无法删除*/ { printf("\nError.\'%s\'isinprotectstatus.Closeitbeforedelete.\n",fname); wgetchar=1; }else { fpaddrno1=ufd[k]->ufdfile[i].fpaddr;//获取文件对应的物理文件名 fpaddrno[fpaddrno1]=0;//回收盘块for(j=i;j<fcount[k];j++) { ufd[k]->ufdfile[j]=ufd[k]->ufdfile[j+1];//从被删除的文件号开场,数组值全部前移一个 } strcpy(str,"d:\\osfile\\file\\file"); itoa(fpaddrno1,str1,10);//整数转化成字符串 strcat(str,str1); strcat(str,".txt"); remove(str);//删除物理文件 fcount[k]--;//用户文件数量少1 printf("\n\'%s\'isdeletedsuccessfully.\n",fname); wgetchar=1; } } }else//所要删除的文件不存在 { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}voidModifyFM()/*ModifyFileMode*/{charfname[MAXNAME],str[50];inti,k;charfmode[25];/*whetherdelete*/if(strcmp(strupr(dirname),strupr(username))!=0) { printf("\nError.Youcanonlymodifyfilemodeinyourselfdir.\n"); wgetchar=1; }else { printf("\nPleaseinputFileName:"); gets(fname); ltrim(rtrim(fname)); i=ExistF(fname);if(i>=0) { k=ExistD(username);if(ifopen[k][i].ifopen==1) { printf("\nError.\'%s\'isinopenstatus.Closeitbeforemodify.\n",fname); wgetchar=1; }else {if(ufd[k]->ufdfile[i].fmode==0)strcpy(str,"readonly");/*FileMode*/elseif(ufd[k]->ufdfile[i].fmode==1)strcpy(str,"writeonly");elseif(ufd[k]->ufdfile[i].fmode==2)strcpy(str,"readandwrite");elsestrcpy(str,"Protect"); printf("\'%s\'filemodeis%s.\n",fname,strupr(str)); printf("Modifyto(0-readonly,1-writeonly,2-readandwrite,3-Protect):"); gets(fmode); ltrim(rtrim(fmode));if(strcmp(fmode,"0")==0) { ufd[k]->ufdfile[i].fmode=0; printf("\n\'%s\'hasbeenmodifiedtoREADONLYmodesuccessfully.\n",fname); wgetchar=1; }elseif(strcmp(fmode,"1")==0) { ufd[k]->ufdfile[i].fmode=1; printf("\n\'%s\'hasbeenmodifiedtoWRITEONLYmodesuccessfully.\n",fname); wgetchar=1; }elseif(strcmp(fmode,"2")==0) { ufd[k]->ufdfile[i].fmode=2; printf("\n\'%s\'hasbeenmodifiedtoREADANDWRITEmodesuccessfully.\n",fname); wgetchar=1; }elseif(strcmp(fmode,"3")==0) { ufd[k]->ufdfile[i].fmode=3; printf("\n\'%s\'hasbeenmodifiedtoFORBIDmodesuccessfully.\n",fname); wgetchar=1; }else { printf("\nError.\'%s\'isnotmodified.\n",fname); wgetchar=1; } } }else { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}voidOpenF()/*OpenFile*/{charfname[MAXNAME];inti,k,j;if(strcmp(strupr(dirname),strupr(username))!=0)/*在自己的目录里才能翻开*/ { printf("\nError.Youcanonlyopeninyourselfdir.\n"); wgetchar=1; }else { k=ExistD(username);for(j=0;j<fcount[k];j++) { printf("%15s",ufd[k]->ufdfile[j].fname); }printf("\nPleaseinputFileName:"); gets(fname);//获取所要翻开的文件名 ltrim(rtrim(fname)); i=ExistF(fname);//获取目录编号if(i>=0) {if(ifopen[k][i].ifopen==1)//输入的文件是翻开的 { printf("\nError.\'%s\'isinopenstatus.\n",fname); wgetchar=1; }else { ifopen[k][i].ifopen=1;//修改为翻开的if(ufd[k]->ufdfile[i].fmode==0)/*根据文件的模式设置翻开模式*/ {ifopen[k][i].openmode=0;}elseif(ufd[k]->ufdfile[i].fmode==1) {ifopen[k][i].openmode=1;}elseif(ufd[k]->ufdfile[i].fmode==2) {ifopen[k][i].openmode=2;}elseifopen[k][i].openmode=3;printf("\n\'%s\'isopenedsuccessfully\n",fname); wgetchar=1; } }else//文件不存在 { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}voidCloseF()/*CloseFile*/{charfname[MAXNAME];inti,k,j;if(strcmp(strupr(dirname),strupr(username))!=0)/*不在自己的目录里没法进展*/ { printf("\nError.Youcanonlyclosefileinyourselfdir.\n"); wgetchar=1; }else { k=ExistD(username);for(j=0;j<fcount[k];j++)/*列出已经翻开的文件*/ {if(ifopen[k][j].ifopen==1)//如果文件是翻开的 printf("%15s",ufd[k]->ufdfile[j].fname); }printf("\nPleaseinputFileName:"); gets(fname);//从键盘上读入所要关闭的文件 ltrim(rtrim(fname)); i=ExistF(fname);//获取文件编号if(i>=0) { ifopen[k][i].ifopen=0;//关闭文件 printf("\n\'%s\'closedsuccessfully\n",fname); wgetchar=1; }else//所要关闭的文件不存在 { printf("\nError.\'%s\'dosenotexist.\n",fname); wgetchar=1; } }}voidReadF()/*ReadFile*/{inti,k,n=0;charfname[MAXNAME];charstr[255],str1[255],c;if(strcmp(strupr(ltrim(rtrim(dirname))),"")==0)//必须在自己的目录内 { printf("\nError.Pleaseconverttoufddirbeforeread.\n"); wgetchar=1;return; } printf("\nCaution:Openfilefirst\n"); printf("OpenedFile(s)List:\n"); k=ExistD(dirname);for(i=0;i<fcount[k];i++) {if(ifopen[k][i].ifopen==1)//如果文件翻开 { printf("%15s",ufd[k]->ufdfile[i].fname); n++; }if((n%4==0)&&(n!=0))printf("\n"); } printf("\n%dfilesopenned.\n",n);if(n==0)wgetchar=1;//如果没有翻开的文件if(n!=0) { printf("\nPleaseinputFileName:"); gets(fname); ltrim(rtrim(fname)); i=ExistF(fname);if(i>=0) {if(ifopen[k][i].ifopen==1) {if((ifopen[k][i].openmode==0)||(ifopen[k][i].openmode==2))//如果文件只读或者读写 { itoa(ufd[k]->ufdfile[i].fpaddr,str,10); strcpy(str1,"file"); strcat(str1,str); strcpy(str,"d:\\osfile\\file\\"); strcat(str,str1); strcat(str,".txt"); fp_file=fopen(str,"rb"); fseek(fp_file,0,0); printf("\nThetextis:\n\n"); printf("");while(fscanf(fp_file,"%c",&c)!=EOF)if(c=='\n')printf("\n");elseprintf("%c",c); printf("\n\n%dLength.\n",ufd[k]->ufdfile[i].flength); fclose(fp_file); wgetchar=1; }elseif(ifopen[k][i].openmode==1)//如果文件只写 { printf("\nError.

温馨提示

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

评论

0/150

提交评论