1、0-Read Only;1-Write Only;2-Read and Write(default) Fname 文件名称用户创建的文件,可以编号存储于磁盘上。如:file0,file1,file2并以编号作为物理地址,在目录中进行登记。本程序需要在c:下建一个名为osfile的目录及一个名为file的子目录,在利用程序创建了文件系统后,可以在这个文件夹下查看到相关的内容。5实验程序#include stdio.hstring.hconio.hstdlib.h#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*/typedef struct /*the structure of OSFILE*/ int fpaddr; /*file physical address*/ int flength; /*file length*/ int fmode; /*file mode:2-Read and Write(default);*/ char fnameMAXNAME; /*file name*/ OSFILE;
3、typedef struct /*the structure of OSUFD*/ char ufdnameMAXNAME; /*ufd name*/ OSFILE ufdfileMAXCHILD; /*ufd own file*/OSUFD;typedef struct /*the structure of OSUFDLOGIN*/ char ufdpword8; /*ufd password*/ OSUFD_LOGIN;typedef struct /*file open mode*/ int ifopen; /*ifopen:0-close,1-open*/ int openmode;
4、/*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; /*the count of mfds ufds*/int fcountMAXCHILD; /*the count of ufds files*/int loginsuc=0; /*whether login successfully*/char usernameMAXNAME; /*record lo
5、gin users name22*/char dirnameMAXNAME;/*record current directory*/int fpaddrnoMAX; /*record file physical address num*/OSUFD_OPENMODE ifopenMAXCHILDMAXCHILD; /*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 choice
6、50; /*choice operation:dir,create,delete,open,delete,modify,read,write*/ int choiceend=1; /*whether choice end*/ char *rtrim(char *str); /*remove the trailing blanks.*/ char *ltrim(char *str); /*remove the heading blanks.*/ void LoginF(); /*LOGIN FileSystem*/ void DirF(); /*Dir FileSystem*/ void CdF
7、(); /*Change Dir*/ void CreateF(); /*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
8、:osfilemfd,rb)=NULL) fp_mfd=fopen(wb); fclose(fp_mfd); for(i=0;i,strupr(dirname); else printf(Bad command or file name.nC:,strupr(username); gets(choice); strcpy(choice,ltrim(rtrim(strlwr(choice); if (strcmp(choice,dir)=0) choice1=1; else if(strcmp(choice,create)=0) choice1=2;delete)=0) choice1=3;at
9、trib)=0) choice1=4;open)=0) choice1=5;close)=0) choice1=6;read)=0) choice1=7;modify)=0) choice1=8;exit)=0) choice1=9;cls)=0) choice1=10;cd)=0) choice1=11;help)=0) choice1=20; else choice1=12; switch(choice1) case 1:DirF();choiceend=1;break; case 2:CreateF();if(!wgetchar) getchar();/创建文件 case 3:Delet
10、eF();wgetchar)getchar();/删除文件 case 4:ModifyFM();/修改文件 case 5:OpenF();if (!/打开文件 case 6:CloseF();/关闭文件 case 7:ReadF();/读文件 case 8:WriteF();/写文件 case 9:printf(nYou have exited this system./提示你已经退出这个系统了 QuitF();exit(0);/退出系统 case 10:clrscr();/清屏 case 11:CdF(); case 20:help();/帮助 default:choiceend=0; el
11、se printf(nAccess denied.void help(void) printf(nThe Command Listn /*printf(nCd Attrib Create Modify Read Open Cls Delete Exit ClosenCreate : Create a file(You can initialize files attribute and content.)nOpen : Open a file to modifynClose : Close a file.nModify : Modify the opened file.nDelete : De
12、lete existed files.nCD : Change current directory.nExit : Exit this program.nchar *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(str, fcountj=0; for(i=0;ufdj-ufdfilei,si
13、zeof(OSFILE),1,fp_ufd)!i+,fcountj+) ifopenji.ifopen=0; ifopenji.openmode=4; fclose(fp_ufd); fclose(fp_mfd); ucount=j; SetPANo(0);nnLogin successful! Welcome to this FileSystemnn loginsuc=1; return; elsenn flag=1; while(flag)Login Failed! Password Error. Try Again(Y/N): gets(a); ltrim(rtrim(a); if (s
14、trcmp(strupr(a),Y)=0) loginsuc=0;flag=0; else if(strcmp(strupr(a),N)=0)loginsuc=0;return; elseNew Password(=8): InputPW(loginpw); /*input new password,use nConfirm Password( InputPW(logincpw); strcpy(ufd_lp.ufdname,strupr(loginame); strcpy(ufd_lp.ufdpword,loginpw);ab fwrite(&ufd_lp,sizeof(OSUFD_LOGI
15、N),1,fp_mfd); strcpy(username,strupr(loginame); strcpy(dirname,loginame); strcpy(str, strcat(str,username); if(fp_ufd=fopen(str, for(i=0; ucount=j; SetPANo(0);nnLogin Successful! Welcome to this Systemnn loginsuc=1; return;void SetPANo(int RorW) /*Set physical address num,0-read,1-write*/ int i,j; if (RorW=0) if(fp_file_p=fopen(osfilefilefile_p fp_file_p=fopen( fclose(fp_file_p);j,sizeof(int),1,fp_file_p)! fpaddrnoj=1; /*for(i=1; if (i%13)=0) fpaddrnoi=1; /*for(i=1; if(i%13)=0) fpaddrnoi=0; for(i=
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2