试验文件管理二.docx
- 文档编号:9808028
- 上传时间:2023-02-06
- 格式:DOCX
- 页数:22
- 大小:55.95KB
试验文件管理二.docx
《试验文件管理二.docx》由会员分享,可在线阅读,更多相关《试验文件管理二.docx(22页珍藏版)》请在冰豆网上搜索。
试验文件管理二
...
实验六:
文件系统
一、目的要求
1、用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程。
从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。
2、要求设计一个n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,且至少有Create、delete、open、close、read、write等命令。
二、例题:
设计一个10个用户的文件系统,每次用户可保存10个文件,一次运行①
用户可以打开5个文件。
程序采用二级文件目录(即设置主目录[MFD])和用户文件目录(UED)。
②另外,为打开文件设置了运行文件目录(AFD)。
为了便于实现,对文件的读写作了简化,在执行读写命令时,只需改读③写指针,并不进行实际的读写操作。
算法与框图:
④a、因系统小,文件目录的检索使用了简单的线性搜索。
b、文件保护简单使用了三位保护码:
允许读写执行、对应位为1,对应位为0,则表示不允许读写、执行。
c、程序中使用的主要设计结构如下:
Ⅰ、主文件目录和用户文件目录(MFD、UFD)
Ⅱ、打开文件目录(AFD)(即运行文件目录)
MDF
AFD
UFD
用户名打开文件名文件名文件目录指针打开保护码保护码
用户名读写指针文件长度
文件目录指针文件名
.
z.
...
三、调度算法的流程图
四、文件管理源程序
#include
#include
#include
#include
typedefstructufd
{charfilename[10];/*文件名*/
charprocode[8];/*属性*/
.
z.
...
intlength;/*文件长度*/
structufd*nextfile;/*指向下一个文件*/
}UFD;
typedefstructmfd
{charusername[10];/*用户名*/
structufd*link;/*指向该用户的第一个文件*/
}MFD;
typedefstructprotected_flag
{charcode[4];
}PRO;
typedefstructafd/*运行文件目录*/
{charfilename[10];/*打开文件名*/
charprocode[4];
intrwpointer;/*读写指针*/
}AFD;
PROflag[3]={@,/*只读*/
H,/*读写*/
\/*可执行*/
};
UFD*rw_pointer;/*读写指针*/
AFD*afd=NULL;
MFDfilesystem[10];
intnum;/*当前用户个数*/
voiddisplayallfile()
{inti;
UFD*p;
for(i=0;i { 牰湩晴尨用户: %s目录如下! \n,filesystem[i].username); p=filesystem[i].link; while(p) {printf(\文件名: %s\t||,p->filename); 牰湩晴尨文件属性: %s\t||,p->procode); 牰湩晴尨文件长度: %d\n\n,p->length); p=p->nextfile; } } . z. ... } voidinput()/*建立用户文件系统*/ {inti,j; intlength; charfilename[10]; charprocode[4]; UFD*p1,*p2,*first; for(i=0;i<10;i++) filesystem[i].link=NULL; 牰湩晴尨用户号码: \n); scanf(%d,&num); for(i=0;i { 牰湩晴尨输入第%d个用户名: \n,i+1); scanf(%s,filesystem[i].username);} for(i=0;i { j=1; printf(第%d个用户的文件如下\n,i+1); 牰湩晴尨文件%d: \n,j); 牰湩晴尨长度: ); scanf(%d,&length); if(! length) continue; 牰湩晴尨文件名: ); scanf(%s,filename); 牰湩晴尨文件属性(100只读,110读写,001可执行)); scanf(%s,procode); first=(UFD*)malloc(sizeof(UFD)); first->nextfile=NULL; strcpy(first->filename,filename); strcpy(first->procode,procode); first->length=length; p1=p2=first; j++; while (1) 灻楲瑮? 文件%d: \n,j); 牰湩晴尨长度: ); scanf(%d,&length); if(! length) break; 牰湩晴尨文件名: ); scanf(%s,filename); . z. ... 牰湩晴尨属性(100只读,110读写,001可执行): ); scanf(%s,procode); p2=(UFD*)malloc(sizeof(UFD)); p2->nextfile=NULL; strcpy(p2->filename,filename); strcpy(p2->procode,procode); p2->length=length; j++; p1->nextfile=p2; p1=p1->nextfile;} filesystem[i].link=first;} displayallfile(); } intfindusername(char*username)/*返回用户名下标,当I=NUM时,用户不存在*/ {inti; i=0; while(i {if(! strcmp(filesystem[i].username,username)) break; i++;} returni; } UFD*findfilename(char*filename,UFD*filepointer) {UFD*k; k=filepointer; while(k&&strcmp(filename,k->filename)) k=k->nextfile; returnk;} voiddisplayfile(UFD*link) {UFD*p; p=link; if(! p) {printf(\ 此目录没有该文件! ); return;} printf(\ 用户文件如下: \n); while(p) {printf(%s\t||,p->filename); printf(%s\t||,p->procode); printf(%d\n\n,p->length); p=p->nextfile;} } . z. ... voidcreatefile() {charusername[10]; charfilename[10]; inti; UFD*p1,*p2,*newfile; if(num>=10)/*用户文件目录已满,创建文件失败*/ { 牰湩晴尨用户文件目录已满\n创建文件失败! \n); return;} 牰湩晴尨输入用户名: \n); scanf(%s,username); i=findusername(username);/*返回用户名下标*/ if(i==num)/*如果该用户不存在,且用户个数不小于10,先创建用户再建立用户文件*/ 灻楲瑮? 用户不存在,请建立用户! \n); strcpy(filesystem[num].username,username);/*创建用户目录*/ num++; {newfile=(UFD*)malloc(sizeof(UFD)); newfile->nextfile=NULL; filesystem[num-1].link=newfile; 牰湩晴尨文件名: ); scanf(%s,filesystem[num-1].link->filename); printf(\ 属性: ); scanf(%s,filesystem[num-1].link->procode); printf(\ 长度: ); scanf(%d,&(filesystem[num-1].link->length)); printf(\ 文件已创建! \n);} } else/*为已存在的用户建立文件*/ {p1=p2=filesystem[i].link; 牰湩晴尨输入文件名: ); scanf(%s,filename); while(p2) {if(! strcmp(p2->filename,filename)) {牰湩晴尨文件名已存在,请输入新文件名! \n); scanf(%s,filename);} p1=p2; p2=p2->nextfile; } newfile=(UFD*)malloc(sizeof(UFD)); newfile->nextfile=NULL; strcpy(newfile->filename,filename); . z. ... printf(\ 属性: ); scanf(%s,newfile->procode); printf(\ 长度: ); scanf(%d,&(newfile->length)); p1->nextfile=newfile;// printf(\ 文件已创建! \n);} displayallfile(); } voiddisplayuser()/*显示用户目录*/ {inti; if(num<=0) return; printf(\ 用户目录如下! ); for(i=0;i printf(\ %s,filesystem[i].username);} voiddeletefile()/*删除文件*/ {inti; charusername[10]; charfilename[10]; UFD*p1,*p2; printf(\ 输入用户名: \n); scanf(%s,username); i=findusername(username);//返回用户名下标 if(i>=num) 灻楲瑮? 用户不存在! \n删除失败! ); return;} else {printf(\ 输入文件名: ); scanf(%s,filename); p1=p2=filesystem[i].link; while(p2&&strcmp(p2->filename,filename)) {p1=p2; p2=p2->nextfile;} if(! p2) printf(\ 文件不存在! \n删除失败! \n); else {if(p1==p2) filesystem[i].link=p1->nextfile; else p1->nextfile=p2->nextfile; delete(p2); 牰湩晴尨文件已删除! \n);} . z. ... } displayallfile();} AFD*openfile() {charusername[10]; charfilename[10]; inti; UFD*p=NULL; AFD*k=NULL;/*为打开的文件设置了运行文件目录*/ 牰湩晴尨输入用户名: \n); scanf(%s,username); i=findusername(username);/*返回用户名下标*/ if(i>=num) {printf(\ 用户不存在! \n不能打开文件! \n); returnNULL;} else/*用户存在*/ {printf(\ 输入文件名: ); scanf(%s,filename); p=findfilename(filename,filesystem[i].link); if(! p)/*用户文件不存在*/ {printf(\ 文件不存在! \n); returnNULL;} else{afd=(AFD*)malloc(sizeof(AFD)); strcpy(afd->filename,p->filename); strcpy(afd->procode,p->procode); afd->rwpointer=0; printf(\ 文件已打开! \n); rw_pointer=p;/*为了读文件保留指针*/ printf(\文件目录AFD: \n);/*打开运行文件目录AFD*/ printf(\文件名: %s\n,afd->filename); printf(\属性: %s\n,afd->procode); printf(\读写指针: %d\n,afd->rwpointer);} } returnafd;} voidclosefile() {if(! afd) {printf(\ 没有文件打开! \n); return;} delete(afd); afd=NULL; printf(\ 文件已关闭! \n);} . z. ... voidreadfile()// {AFD*p; p=openfile(); if(! p) return; if(strcmp(p->procode,@)&&strcmp(p->procode,H)) printf(\ file: %s文件不可读! \n,p->filename);/*操作*/ else {牰湩晴尨已读取文件! \n); printf(\文件名: %s\n,p->filename); printf(\属性: %s\n,p->procode); printf(\读写指针: %d\n,rand()%rw_pointer->length);} closefile();/*关闭文件*/ } voidwritefile() {AFD*p;/*运行文件目录*/ UFD*k; p=openfile(); k=rw_pointer; if(! p) return; if(strcmp(p->procode,H)) {printf(\ file: %s该文件不能写! \n,p->filename); return;} else {p->rwpointer=rand()%k->length; k->length=p->rwpointer;/*修改读书指针写回文件*/ } printf(\ 已完成写操作! \n); printf(\文件名: %s\n,k->filename); printf(\属性: %s\n,k->procode); printf(\长度: %d\n,k->length); closefile(); } voidexecutefile() {AFD*p; p=openfile(); if(! p) {printf(\ 文件不存在! \n); return;} if(strcmp(p->procode,)) {printf(\ 该文件不能执行! \n); . z. ... printf(\ 文件属性为不可执行文件! \n); return;} else{ closefile(); printf(\ 文件执行完毕! );} } voidmenu() {printf(------------------------------------------------------------------------------\n); printf(<文件管理> \n); printf(------------------------------------------------------------------------------\n); printf(MENU: \n); printf(\\tmenu----------显示菜单\n); printf(\\topen----------打开文件\n); printf(\\tclose---------关闭文件\n); printf(\\tcreate--------创建文件\n); printf(\\tread----------读文件\n); printf(\\twrite---------写文件\n); printf(\\texecute-------执行文件\n); printf(\\tdelete--------删除文件\n); printf(\\tdisplay--------显示文件列表\n); printf(\\texit--------退出\n); printf(\注意: 输入时当文件长度为0时表示该用户的文件输入结束! \n); printf(------------------------------------------------------------------------------\n); } voidfileoperation()/*文件基本操作*/ {charstr[10]; intsort; while (1) 灻楲瑮? 输入命令: \n); printf(>>); scanf(%s,str); if(! strcmp(str,create))sort=1; else if(! strcmp(str,delete))sort=2; else if(! strcmp(str, ead))sort=3; else if(! strcmp(str,write))sort=4; else if(! strcmp(str,execute))sort=5; else . z. ... if(! strcmp(str,open))sort=6; else if(! strcmp(str,close))sort=7; else if(! strcmp(str,menu))sort=8; else if(! strcmp(str,display))sort=9; else if(! strcmp(str,exit))sort=10; else sort=0; switch(sort) {case1: createfile();break; case2: deletefile();break; case3: readfile();break; case4: writefile();break; case5: executefile();break; case6: openfile();break; case7: closefile();break; case8: menu();break; case9: displayallfile();break; case10: return; default: break;} } } voidmain() {menu(); input(); fileoperation(); } . z.
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 试验 文件 管理
![提示](https://static.bdocx.com/images/bang_tan.gif)