学号:姓名:协作者:________实验__四__题目__文件管理___第周星期___一、实验目的:通过具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。二、预习与参考:教材中文件系统的相关知识。三、实验要求(设计要求)1.要求设计一个n个用户的文件系统,文件目录采用两级结构;2.对文件必须设置保护措施;3.至少有Create、delete、open、close、read、write等命令。四、完成情况1)设计思想说明程序要求每个用户在登陆后才可对其拥有的文件进行操作,用户对于其他用户的文件无操作权.文件操作包括浏览、创建、删除、打开、关闭、阅读、写入、修改模式.其他操作包括新建用户、帮助、用户登入、用户登出、退出系统.在程序文件夹下有个名为“file”的系统根目录,此目录下包括:一个名为“mfd”的文件,记录所有注册过的帐号及密码;用户文件,以用户名作为文件名,内容为其拥有的文件名及属性;一个名为“keiji”的文件夹.“keiji”文件夹中包括:“file.p”指针文件,记录所有已用的物理地址;一些以物理地址为名的文件,内容为文件内容.2)数据结构的说明file结构体系统文件数据结构:fpaddrint,文件的物理地址、flengthint,文件长度、fmodeint,文件模式0.只读;1.可写;2.可读写;3.保护、fname[]char,文件名;filemode结构体文件状态数据结构:isopenint,文件当前状态,0.关闭;1.打开、modeint,文件模式0.只读;1.可写;2.可读写;3.初始化;user结构体用户信息数据结构:uname[]char,用户名、upassword[]char,用户密码;userfile结构体用户文件数据结构:uname[]char,用户名、ufile[]file,用户拥有的文件数组.3)各主要模块的算法流程图总体流程图:各个命令的流程图:开始输入用户名在MFD中找到该用户?输入操作命令显示该用户目录表UFD中的所有文件是什么命令?保存当前文件目录是delete初始化运行文件表AFD建立文件处理程序删除文件处理程序打开文件处理程序关闭文件处理程序读出文件处理程序写入文件处理程序结束报告无此用户文件否createopenbyeclosereadwrite创建文件:删除文件:打开文件:读文件:写文件:关闭文件:4)程序清单#includestdio.h#includestdlib.h#includeconio.h#includetime.h#includestring.h#defineMaxUser100//定义最大MDF主目录文件#defineMaxDisk512*1024//模拟最大磁盘空间#definecommandAmount12//对文件操作的指令数//存储空间管理有关结构体和变量chardisk[MaxDisk];//模拟512K的磁盘存储空间运行文件队列不为空将运行文件队列中的结点转接到用户文件队列输入文件名从运行文件队列中查找到该文件写该文件输入文件名从运行文件队列中查找到该文件读该文件输入文件名从文件队列中查找到该文件判断是否允许操作将打开文件加入到运行文件队列输入文件名从文件队列中查找到该文件删除该文件申请空间输入文件名初始化文件插入文件队列typedefstructdistTable//磁盘块结构体{intmaxlength;intstart;intuseFlag;distTable*next;}diskNode;diskNode*diskHead;structfileTable//文件块结构体{charfileName[10];intstrat;//文件在磁盘存储空间的起始地址intlength;//文件内容长度intmaxlength;//文件的最大长度charfileKind[3];//文件的属性--读写方式structtm*timeinfo;boolopenFlag;//判断是否有进程打开了该文件//fileTable*next;};//两级目录结构体typedefstructuser_file_directory//用户文件目录文件UFD{//charfileName[10];fileTable*file;user_file_directory*next;}UFD;//UFD*headFile;typedefstructmaster_file_directory//主文件目录MFD{charuserName[10];charpassword[10];UFD*user;}MFD;MFDuserTable[MaxUser];intused=0;//定义MFD目录中用已有的用户数//文件管理voidfileCreate(charfileName[],intlength,charfileKind[]);//创建文件voidfileWrite(charfileName[]);//写文件voidfileopen(charfileName[]);//读文件voidfileDir(charUserName[]);//显示某一用户的所有文件voidfileClose(charfileName[]);//关闭已打开的文件voidfileDel(charfileName[]);//删除文件voidchmod(charfileName[],charkind[]);//修改文件的读写方式intrequestDist(int&startPostion,intmaxLength);//磁盘分配查询voidinitDisk();//初始化磁盘voidfreeDisk(intstartPostion);//磁盘空间释放//用户管理voiduserCreate();intlogin();intuserID=-1;//用户登录的ID号,值为-1时表示没有用户登录intmain(){charorder[commandAmount][10];strcpy(order[0],create);strcpy(order[1],rm);strcpy(order[2],open);strcpy(order[3],write);strcpy(order[5],chmod);strcpy(order[7],dir);strcpy(order[8],close);strcpy(order[9],return);strcpy(order[10],exit);charcommand[50],command_str1[10],command_str2[10],command_str3[5],command_str4[3];inti,k,j;intlength;initDisk();//初始化磁盘for(i=0;iMaxUser;i++)//初始化用户UFD目录文件的头指针{userTable[i].user=(UFD*)malloc(sizeof(UFD));userTable[i].user-next=NULL;}while(1){printf(********************************************\n);printf(*****************3110005956*****************\n);printf(*******************吴梓超*******************\n);printf(1、创建用户\n);printf(2、登陆\n);printf(********************************************\n);printf(Pleasechoocethefunctionkey:);intchoice;scanf(%d,&choice);if(choice==1)userCreate();elseif(choice==2)userID=login();elseprintf(您的输入有误,请重新选择\n);while(userID!=-1){fflush(stdin);printf(---------------------------------------\n);printf(create-创建--------格式:createA500rw,将创建名为A,长度为500字节可读可写的文件\n);printf(rm-----删除--------格式:rmA,将删除名为A的文件\n);printf(open---查看文件-----格式:openA,显示A的内容\n);printf(write--写入-------格式:writeA\n);printf(chmod--修改--------格式:chmodAr,将文件A的权限改为只读方式\n);printf(dir----显示文件----格式:diraaa,将显示aaa用户的所有文件\n);printf(close--关闭文件----格式:closeA,将关闭文件A\n);printf(return-切换用户\n);printf(exit---退出程序\n);printf(----------------------------------------\n);printf(pleaseimputyourcommand:);gets(command);intselect;for(i=0;command[i]!=''&&command[i]!='\0';i++)//command_str1字符串存储命令的操作类型command_str1[i]=command[i];k=i;command_str1[k]='\0';for(i=0;icommandAmount;i++){if(!strcmp(command_str1,order[i])){select=i;break;}}if(i==commandAmount){printf(您输入的命令有误,请重新输入\n);continue;}for(i=k+1,k=0;command[i]!=''&&command[i]!='\0';i++,k++)//commmand_str2字符串存储文件名或用户名command_str2[k]=command[i];command_str2[k]='\0';k=i;switch(select){case0:for(i=k+1,k=0;command[i]!='';i++,k++)command_str3[k]=command[i];command_str3[k]='\0';k=i;j=1;length=0;//初始化文件长度for(i=strlen(command_str3)-1;i=0;i--)//把字符串转换为十进制{length+=(command_str3[i]-48)*j;j*=10;}for(i=k+1,k=0;command[i]!=''&&command[i]!='\0';i++,k++)command_str4[k]=command[i];command_str4[k]='\0';fileCreate(command_str2,length,command_str4);break;case1:fileDel(command_str2);break;case2:fileopen(command_str2);break;case3:fileWrite(command_str2);break;case5:for(i=k+1,k=0;command[i]!=''&&command[i]!='\0';i++,k++)command_str3[k]=command[i];command_str3[k]='\0';chmod(command_str2,command_str3);break;case7:fileDir(command_str2);break;case8:fileCl