
操作系统文件操作.pdf
20页1 实验四文件操作一、实验名称:简单文件系统的实现二、实验目的要求在 TC、VB、Delphi、C+Builder等语言与开发环境中,实现对文件的各项操作(复制、打开、保存、录入、查找、显示属性等);使学生进一步了解文件的主要操作三、实验内容为 DOS 系统设计一个简单的二级文件系统要求做到以下几点:可以实现下列几条命令LOGIN 用户登陆DIR 列文件目录CREATE 创建文件DELETE 删除文件OPEN 打开文件CLOSE 关闭文件READ 读文件WRITE 写文件MODIFY 修改文件内容列目录时要列出文件名,物理地址,保护码和文件长度源文件可以进行读写保护四、实验提示首先应确定文件系统的数据结构:主目录、子目录及活动文件等主目录和子目录都以文件的形式存放于磁盘,这样便于查找和修改用户创建的文件,可以编号存储于磁盘上如: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 largest 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 fmode;/*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;/*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;/*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 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 choice50;/*choice operation:dir,create,delete,open,delete,modify,read,write*/int choiceend=1;/*whether choice end*/char*rtrim(char*str);/*remove 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 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:osfilemfd,rb)=NULL)fp_mfd=fopen(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)choice1=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=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(!wgetchar)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(!wgetchar)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 denied.);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(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);printf(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 printf(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,sizeof(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 。





![河南新冠肺炎文件-豫建科[2020]63号+豫建科〔2019〕282号](http://img.jinchutou.com/static_www/Images/s.gif)






