文件管理系统源代码_第1页
文件管理系统源代码_第2页
文件管理系统源代码_第3页
文件管理系统源代码_第4页
文件管理系统源代码_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、文件管理系统源代码文件管理系统一、实验目的通过设计一个多用户文件系统, 了解操作系统中文件的组织与管理, 熟悉文件管理所用 的数据结构,加深对文件系统内部功能实现过程的理解。二、实验内容1用 C 语言或 C+ 语言设计一个最多包括 N 个用户的多用户文件系统, 约定每个用户 最多保存 M 个文件。同时限制一个用户在进入系统后,最多打开 L 个文件。2系统应具备一定的健壮性。即能够检查用户所输入命令的正确性,出错时显示出必 要的信息。另外,对文件需设置必要的保护措施。3文件目录结构采用两级目录结构 : 主文件目录和用户文件目录/* 用户数 */* 一个用户可保存 M 个文件 */ /*用户只能一

2、次打开 L 个文件 */ /* 主文件目录 */#includeio.h #includeconio.h #includestdio.h #includestdlib.h #includemalloc.h #includestring.h #includectype.h #define N 30 #define M 20 #define L 5 typedef struct MFD char username100;char password100;FILE fp;/*文件指针 */MFD;/typedef struct UFD /* 用户文件目录 */ char filename256;ch

3、ar protect;/* 保护码 */int length;/* 文件长度 */ UFD;/* 打开文件目录 */ typedef struct OFDchar filename256;char opencode; /* 打开保护码 */ int fp; /* 读写指针 */OFD;/ typedef struct COMM /* 命令串 */char string256; /* 命令 */struct COMM *next;/* 后继指针 */ COMM;/* 主文件目录数组 */* 用户文件目录数组 */* 打开文件目录数组 */* 命令串指针 */MFD mainfdN;UFD use

4、rfdM;OFD openfdL;/ COMM*command; char username10;int usernum,savenum,opennum; int workfile;void init();void init_ufd(char *username);/* void mesg(char *str);char *getpass();char *getuser();COMM *readcommand();void login();void logout();void setpass();void create();void mydelete();void myread();void

5、myopen();void myclose();void mywrite();void help();void dir();void mycopy();void myrename();/初始化用户文件目录 */ /*消息 */* 设置口令函数声明 */* 设置用户函数声明 */* 读命令串函数声明 */* 用户登录 */* 用户注销 */* 设置口令 */* 创建文件 */* 删除文件 */* 读文件 */* 打开文件 */* 关闭文件 */* 写文件 */*帮助 */* 列文件目录 */* 复制文件 */* 重命名文件名 */void main()init();for(;)readcomma

6、nd();if(strcmp(command-string,create)=0)create();else if(strcmp(command-string,delete)=0)文件管理系统源代码文件管理系统源代码mydelete();else if(strcmp(command-string,open)=0) myopen();else if(strcmp(command-string,close)=0) myclose();else if(strcmp(command-string,read)=0) myread();else if(strcmp(command-string,write)

7、=0) mywrite();else if(strcmp(command-string,copy)=0) mycopy();else if(strcmp(command-string,rename)=0) myrename();else if(strcmp(command-string,login)=0) login();else if(strcmp(command-string,setpass)=0) setpass();else if(strcmp(command-string,logout)=0) logout();else if(strcmp(command-string,help)=

8、0) help();else if(strcmp(command-string,dir)=0) dir();else if(strcmp(command-string,exit)=0) break;elsemesg(Bad command!);/void init()FILE *fp;char tempname20,temppass20;int i=0;usernum=0;savenum=0;opennum=0;strcpy(username,); if(fp=fopen(mainfile.txt,r)!=NULL) while(!feof(fp)strcpy(tempname,);fgets

9、(tempname,20,fp);if(strcmp(tempname,)!=0)fgets(temppass,20,fp); tempnamestrlen(tempname)-1=0; temppassstrlen(tempname)-1=0; strcpy(mainfdusernum.username,tempname); strcpy(mainfdusernum.password,tempname); usernum+;fclose(fp);/void init_ufd(char *username)/* 初始化用户文件目录 */FILE *fp;char tempfile100,tem

10、pprot;int templength;savenum=0;opennum=0;workfile=-1;if(fp=fopen(username,w+)!=NULL)while(!feof(fp)strcpy(tempfile,);fgets(tempfile,50,fp);if(strcmp(tempfile,)!=0)fscanf(fp,%c,&tempprot); fscanf(fp,%d,&templength); tempfilestrlen(tempfile)-1=0; strcpy(userfdsavenum.filename,tempfile); userfdsavenum.

11、protect=tempprot; userfdsavenum.length=templength; savenum+;fgets(tempfile,50,fp);文件管理系统源代码fclose(fp);/void mesg(char *str)printf(n %sn,str);/char *getuser()char username20;char temp;int i;username0=0;for(i=0;i10;)while(!kbhit();temp=getch();if(isalnum(temp)|temp=_|temp=13) usernamei=temp; if(userna

12、mei=13)usernamei=0; break;putchar(temp);i+;usernamei=0;return(username);/char *getpass()char password20;char temp;int i;password0=0;for(i=0;i);gets(line);for(i=0;i0)tempend=0;newp=(COMM *)malloc(sizeof(COMM *);文件管理系统源代码strcpy(newp-string,temp);newp-next=NULL;if(command=NULL) command=newp;elsep=comma

13、nd;while(p-next!=NULL) p=p-next;p-next=newp; p=command;return(command);/void login() /* 用户注册 */FILE *fp;int i;char password20,confirm20,tempname20; if(command-next=NULL)printf(n User Name:); strcpy(tempname,getuser();else if(command-next-next!=NULL)mesg(Too many parameters!);return;else strcpy(tempn

14、ame,command-next-string);for(i=0;i=usernum)/* 新用户 */printf(n new user account,enter your password twice!); printf(n Password:);strcpy(password,getpass();/* 输入口令且返回之 */ printf(n Password:);strcpy(confirm,getpass(); /* 第二次输入口令 */ if(strcmp(password,confirm)=0)/* 用户数不能超过 N*/ if(usernum=N)mesg(Create ne

15、w account false!number of user asscount limited.n login fasle!);elsestrcpy(mainfdusernum.username,tempname);/* 把新用户和口令填入 mainfd 中 */strcpy(mainfdusernum.password,password); usernum+;strcpy(username,tempname);mesg(Create a new user!n login success!);init_ufd(username);/* 初始化用户文件目录 */fp=fopen(mainfile

16、.txt,w+);/* 把新用户填入 mainfile.txt 文件中 */for(i=0;inext!=NULL)mesg(Too many parameters!);else if(strcmp(username,)=0)mesg(No user login!);else strcpy(username,); opennum=0; savenum=0; workfile=-1; mesg(User logout!);/void setpass() /* 修改口令 */int i=0;FILE *fp;char oldpass20,newpass20,confirm20; if(strcmp

17、(username,)=0) mesg(No user login!);else printf(n Old password:); strcpy(oldpass,getpass(); for(int i=0;iusernum;i+) if(strcmp(mainfdi.username,username)=0) break; if(strcmp(mainfdi.password,oldpass)!=0) mesg(Old password error!);elseprintf(n New password:); strcpy(newpass,getpass(); printf(n Confir

18、m password:);文件管理系统源代码strcpy(confirm,getpass(); if(strcmp(newpass,confirm)!=0)mesg(Password not change! confirm different.);else strcpy(mainfdi.password,newpass); mesg( Password change !);fp=fopen(mainfile.txt,w+); for(i=0;inext=NULL) mesg(Too few parametets!);else if(command-next-next!=NULL)mesg(To

19、o many parameters!);elsefor(i=0;inext-string)=0) break;if(i=M)mesg(Error! connot create file! number of files limited!); else文件管理系统源代码strcpy(userfdsavenum.filename,command-next-string); tect=r;userfdi.length=rand();savenum+; mesg(Create file success!); fp=fopen(username,w+); for(i=0;inext

20、=NULL) mesg(Too few parametets!);else if(command-next-next!=NULL) mesg(Too many parameters!);else for(i=0;inext-string)=0) break;if(i=savenum)mesg(Error! the file not existed!); else tempsave=i; for(i=0;inext-string,openfdi.filename)=0) break; if(i=opennum) mesg(File not open);/else if(tempsave=save

21、num-1) savenum-;else for(;tempsavesavenum-1;tempsave+) strcpy(userfdtempsave.filename,userfdtempsave+1.filename); tect=userfdtempsave+1.protect; userfdtempsave.length=userfdtempsave+1.length;savenum-; mesg(Delete file success!); fp=fopen(username,w+);for(i=0;inext=NULL)mesg(Too few

22、 parametets!);else if(command-next-next!=NULL)mesg(Too many parameters!);elsestrcpy(tempfile,command-next-string); for(i=0;i=savenum) mesg(File not existed!);elsetempsave=i;for(i=0;i=opennum) mesg(File not opened);else if(userfdtempsave.lengthnext=NULL)mesg(Too few parameters!);文件管理系统源代码elsestrcpy(t

23、empfile,);tempcode=r;if(strcmp(command-next-string,/r)=0)tempcode=r;type=1;else if(strcmp(command-next-string,/w)=0)tempcode=w;type=1;else if(strcmp(command-next-string,/d)=0)tempcode=d;type=1;else if(command-next-string0=/)mesg(Error! /r/w/d request!);else if(command-next-next!=NULL)mesg(Too many p

24、arameters!);elsestrcpy(tempfile,command-next-string);if(type=1)if(command-next-next!=NULL)if(command-next-next-next!=NULL) mesg(Too many parameters!);else strcpy(tempfile,command-next-next-string);elsemesg(Too few parameters!);if(strcmp(tempfile,)for(i=0;i=savenum)mesg(File not existed!);elsefor(i=0

25、;iopennum;i+)if(strcmp(tempfile,openfdi.filename)=0)break;if(i=L)mesg(Error! connot open file! number of opened files limited!);else strcpy(openfdopennum.filename,tempfile); openfdopennum.opencode=tempcode; workfile=opennum;opennum+;mesg(File open success!);/void myclose()/* 关闭 */int i=0;int tempsav

26、e=0;char tempfile100;if(strcmp(username,)=0)mesg(No user lgoin!);else if(command-next=NULL)mesg(Too few parametets!);else if(command-next-next!=NULL)mesg(Too many parameters!);else strcpy(tempfile,command-next-string); for(i=0;i=savenum) mesg(File not existed!);elsefor(i=0;i=opennum) mesg(File not o

27、pened);elseif(i=opennum-1) opennum-;else for(;inext=NULL) mesg(Too few parametets!);else if(command-next-next!=NULL) mesg(Too many parameters!);else strcpy(tempfile,command-next-string);for(i=0;i=savenum) mesg(File not existed!);elsetempsave=i;for(i=0;i=opennum) mesg(File not opened);elsemesg(File w

28、rite);int tt=rand();if(userfdtempsave.length=1000)printf(n The file size from %d KB to %d KB,userfdtempsave.length,userfdtempsave.length+tt);/* else if(userfdtempsave.lenght=1000)printf(n The file size from 1000 KB);*/ elseprintf(n The file size form %d,%d KB to %d,%d KB,userfdtempsave.length/1000,u

29、serfdtempsave.length%1000,userfdtempsave.length+tt/10 00,userfdtempsave.length+tt%1000);userfdtempsave.length=userfdtempsave.length+tt;文件管理系统源代码/void help()/* 帮助 */static char *cmd=login,setpass,logout,create, open,read,write,close,delete,dir, help,exit,copy,rename;static char *cmdhlp=aommand format

30、: login ,command format:setpass,command format:logout,command format:create ,command format:open /r/w/d ,command format:read ,command format:write ,command format:close ,command format:delete ,command format:dir /u/o/f,command format:help ,command format:exit,command format:copy ,command format:rena

31、me ;static char *detail=explain:user login in the file system.,explain:modify the user password.,explain:user logout the file system.,explain:creat a new file.,explain:/r - read only deflautnt/w - list opened files nt/d -read,modify and delete.,explain:read the file.,explain:modify the file.,explain

32、:close the file.,explain:delete the file.,explain:/u - list the user accountnt/0 - list opened filesnt/f -list user filedeflaut.,explain: - list the command detail format and explain.nt deflaut list the command.,explain:exit from the file sysytem.,explain:copy from one file to another file.,explain:

33、modify the file name.;int helpnum=14;int i=0;if(command-next=NULL)mesg(cmdhlp10);mesg(detail10);mesg(step 1: login);printf(t if old user then login,if user not exist then create new user);mesg(step 2: open the file for read(/r),write(/w)or delete(/d);printf(t you can open one or more files.one comma

34、nd can open one file); mesg(step 3: read,write or delete some one file);printf(t you can operate one of the opened files.one command can open one file); mesg(step 4: close the open file);printf(t you can close one of the opened files.one command can open one file); mesg(step 5: user logout.close all

35、 the user opened files);printf(n command list:);for(i=0;inext-next!=NULL)mesg(Too many parameters!);elsefor(i=0;inext-string,cmdi)=0) break;if(i=helpnum)mesg(the command not existed!);elsemesg(cmdhlpi); mesg(detaili);/void dir()/* 列目录文件 */int i=0;int type=0;char tempcode;if(strcmp(username,)=0)mesg(

36、No user login!);elseif(command-next=NULL)tempcode=f;else if(strcmp(command-next-string,/u)=0) tempcode=u;else if(strcmp(command-next-string,/o)=0)tempcode=o;else if(strcmp(command-next-string,/f)=0)tempcode=f;else if(command-next-string0=/) mesg(Error! /u/o/f request!);else if(command-next-next!=NUL

37、L) mesg(Too many parameters!);if(u=tempcode)printf(list the user accountn); printf(usenamen);for(i=0;iusernum;i+) printf(%sn,mainfdi.username);else if(f=tempcode)printf(list opened filesn); printf(filename length protectn); for(i=0;isavenum;i+) printf(%s%s%d%s%s%cn,userfdi.filename, ,userfdi.length,

38、KB, ,tect);else if(o=tempcode)printf(list user filesn);printf(filename opencoden); for(i=0;inext=NULL)mesg(Too few parametets!);else if(command-next-next=NULL)mesg(Too few parametets!);else if(command-next-next-next!=NULL)mesg(Too many parameters!);elsestrcpy(sourfile,command-next-string)

39、; strcpy(destfile,command-next-next-string); for(i=0;i=savenum)printf(n the source file not eixsted!);else for(i=0;i=opennum)printf(n the source file not opened!);elsefor(j=0;jopennum;j+)if(strcmp(destfile,openfdi.filename)=0)break;if(jopennum)mesg(Error! the destination file opened,you must close it f

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论