当前位置:文档之家› 实验四 文件系统实验

实验四 文件系统实验

实验四文件系统实验一 . 目的要求1、用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。

从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。

2、要求设计一个 n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,且至少有Create、delete、open、close、read、write等命令。

二 . 例题:1、设计一个10个用户的文件系统,每次用户可保存10个文件,一次运行用户可以打开5个文件。

2、程序采用二级文件目录(即设置主目录[MFD])和用户文件目录(UED)。

另外,为打开文件设置了运行文件目录(AFD)。

3、为了便于实现,对文件的读写作了简化,在执行读写命令时,只需改读写指针,并不进行实际的读写操作。

4、算法与框图:①因系统小,文件目录的检索使用了简单的线性搜索。

②文件保护简单使用了三位保护码:允许读写执行、对应位为 1,对应位为0,则表示不允许读写、执行。

③程序中使用的主要设计结构如下:主文件目录和用户文件目录( MFD、UFD)打开文件目录( AFD)(即运行文件目录)文件系统算法的流程图如下:三 . 实验题:1、增加 2~3个文件操作命令,并加以实现。

(如移动读写指针,改变文件属性,更换文件名,改变文件保护级别)。

#include <stdio.h>#include <stdlib.h>#include <string.h>#define getpch(type) (type*)malloc(sizeof(type))int userNum=0;struct mdf{char userName[20];struct UFD* p;} mdf[20];struct ufd{char fileName[20];char File[50];struct ufd * next;}*fp,*tp,*p,*begin;typedef struct ufd UFD ;void show(struct UFD *f){begin=f;if(begin->next==NULL) printf("该用户名下尚无文件!\n");else{printf("该用户名下所有文件:\n");begin=begin->next;while(begin!=NULL){printf("%s: %s\n",begin->fileName,begin->File);begin=begin->next;}}}void Operation(struct UFD *f){int i;char filename[20],file[50];begin=f;label:printf("请选择操作:\n 1:create; 2:delete; 3:read; 4:write; 5:open;\n 6:lose; 7:Chang File's Name; 8:Show All The File\n"); scanf("%d",&i);if(i==1){tp=getpch(UFD);printf("请输入文件名:");scanf("%s",filename);printf("\n请输入文件内容:");scanf("%s",file);strcpy(tp->fileName,filename);strcpy(tp->File,file);tp->next=NULL;p=begin;p->next=tp;printf("\n文件创建完毕!\n");goto label;}else if(i==2){printf("请输入文件名:");scanf("%s",filename);p=begin->next;while(strcmp(p->fileName,filename)!=0&&p!=NULL) p=p->next;if(p==NULL) printf("文件不存在!\n");else{tp=begin;while(tp->next!=p) tp=tp->next;tp->next=p->next;free(p);printf("文件已删除!\n");}goto label;}else if(i==3){printf("请输入文件名:");scanf("%s",filename);p=begin->next;while(strcmp(p->fileName,filename)!=0&&p!=NULL) p=p->next;if(p==NULL) printf("文件不存在!\n");else{printf("%s: %s\n",p->fileName,p->File);}goto label;}else if(i==4){printf("请输入文件名:");scanf("%s",filename);printf("\n请输入文件内容:");scanf("%s",file);p=begin->next;while(p!=NULL){if(!(strcmp(p->fileName,filename))){strcpy(p->File,file);printf("\n替换了以%s为名的文件!\n",filename); goto label;}p=p->next;}tp=getpch(UFD);strcpy(tp->fileName,filename);strcpy(tp->File,file);tp->next=NULL;p=begin;p->next=tp;printf("\n创建了以%s为名的文件!\n",filename);goto label;}else if(i==5){goto label;}else if(i==6){printf("功能被关闭,无法操作了\n");Select();}else if(i==7){printf("请输入要改名的文件名:");scanf("%s",filename);while(p!=NULL){if(!(strcmp(p->fileName,filename))){printf("\n请输入新的文件名:");scanf("%s",filename);strcpy(p->fileName,filename);printf("\n文件名已更改!\n");goto label;}p=p->next;}printf("文件不存在!\n");goto label;}else if(i==8){show(f);goto label;}else{goto label;}}void Select(){char username[20];int i;printf("请输入用户名:\n");scanf("%s",username);for(i=0; i<userNum; i++){if(!strcmp(mdf[i].userName,username)){fp= mdf[i].p;if(fp!=NULL){printf("该用户已创建文件:\n");while(fp!=NULL){fp=fp->next;printf("%s\n",fp->fileName);}}else{printf("该用户尚未创建任何文件!\n"); }fp= mdf[i].p;Operation(fp);}}if(i>=userNum){printf("该用户不存在,创建新用户?\n 1:是 2:否\n");scanf("%d",&i);if(i==1){strcpy(mdf[userNum++].userName,username);printf("已创建用户!\n");i=userNum-1;fp= mdf[i].p;Operation(fp);}else{printf("查询其它?\n 1:是 2:否\n");scanf("%d",&i);if(i==1){Select();}else{printf("谢谢使用!\n");return;}}}}int main(){int i;for(i=0; i<20; i++){tp=getpch(UFD);tp->next=NULL;mdf[i].p=tp;}Select();return 0;}2、编一个通过屏幕选择命令的文件管理系统,每屏要为用户提供足够的选择信息,不需要打入冗长的命令。

#include <stdio.h>#include <stdlib.h>#include <string.h>#define getpch(type) (type*)malloc(sizeof(type))int userNum=0;struct mdf{char userName[20];struct UFD* p;} mdf[20];struct ufd{char fileName[20];char File[50];struct ufd * next;}*fp,*tp,*p,*begin;typedef struct ufd UFD;void show(struct UFD *f){begin=f;if(begin->next==NULL) printf("该用户名下尚无文件!\n");else{printf("该用户名下所有文件:\n");begin=begin->next;while(begin!=NULL){printf("%s: %s\n",begin->fileName,begin->File);begin=begin->next;}}}void Operation(struct UFD *f){int i;char filename[20],file[50];begin=f;label:printf("请选择操作:\n 1:create; 2:delete; 3:read; 4:write; 5:open;\n 6:lose; 7:Chang File's Name; 8:Show All The File\n"); scanf("%d",&i);if(i==1){tp=getpch(UFD);printf("请输入文件名:");scanf("%s",filename);printf("\n请输入文件内容:");scanf("%s",file);strcpy(tp->fileName,filename);strcpy(tp->File,file);tp->next=NULL;p=begin;p->next=tp;printf("\n文件创建完毕!\n");goto label;}else if(i==2){printf("请输入文件名:");scanf("%s",filename);p=begin->next;while(strcmp(p->fileName,filename)!=0&&p!=NULL) p=p->next;if(p==NULL) printf("文件不存在!\n");else{tp=begin;while(tp->next!=p) tp=tp->next;tp->next=p->next;free(p);printf("文件已删除!\n");}goto label;}else if(i==3){printf("请输入文件名:");scanf("%s",filename);p=begin->next;while(strcmp(p->fileName,filename)!=0&&p!=NULL) p=p->next;if(p==NULL) printf("文件不存在!\n");else{printf("%s: %s\n",p->fileName,p->File);}goto label;}else if(i==4){printf("请输入文件名:");scanf("%s",filename);printf("\n请输入文件内容:");scanf("%s",file);p=begin->next;while(p!=NULL){if(!(strcmp(p->fileName,filename))){strcpy(p->File,file);printf("\n替换了以%s为名的文件!\n",filename); goto label;}p=p->next;}tp=getpch(UFD);strcpy(tp->fileName,filename);strcpy(tp->File,file);tp->next=NULL;p=begin;p->next=tp;printf("\n创建了以%s为名的文件!\n",filename);goto label;}else if(i==5){goto label;}else if(i==6){printf("功能被关闭,无法操作了\n");Select();}else if(i==7){printf("请输入要改名的文件名:");scanf("%s",filename);while(p!=NULL){if(!(strcmp(p->fileName,filename))){printf("\n请输入新的文件名:");scanf("%s",filename);strcpy(p->fileName,filename);printf("\n文件名已更改!\n");goto label;}p=p->next;}printf("文件不存在!\n");goto label;}else if(i==8){show(f);goto label;}else{goto label;}}void Select(){char username[20];int i;printf("请输入用户名:\n");scanf("%s",username);for(i=0; i<userNum; i++){if(!strcmp(mdf[i].userName,username)){fp= mdf[i].p;if(fp!=NULL){printf("该用户已创建文件:\n");while(fp!=NULL){fp=fp->next;printf("%s\n",fp->fileName);}}else{printf("该用户尚未创建任何文件!\n"); }fp= mdf[i].p;Operation(fp);}}if(i>=userNum){printf("该用户不存在,创建新用户?\n 1:是 2:否\n");scanf("%d",&i);if(i==1){strcpy(mdf[userNum++].userName,username);printf("已创建用户!\n");i=userNum-1;fp= mdf[i].p;Operation(fp);}else{printf("查询其它?\n 1:是 2:否\n");scanf("%d",&i);if(i==1){Select();}else{printf("谢谢使用!\n");return;}}}}int main(){int i;for(i=0; i<20; i++){tp=getpch(UFD);tp->next=NULL;mdf[i].p=tp;}Select();return 0;}3、设计一个树型目录结构的文件系统,其根目录为 root,各分支可以是目录,也可以是文件,最后的叶子都是文件。

相关主题