文件管理系统源代码.docx
《文件管理系统源代码.docx》由会员分享,可在线阅读,更多相关《文件管理系统源代码.docx(33页珍藏版)》请在冰豆网上搜索。
![文件管理系统源代码.docx](https://file1.bdocx.com/fileroot1/2023-2/9/5979f044-35dc-46a9-b3ab-0789166b6f52/5979f044-35dc-46a9-b3ab-0789166b6f521.gif)
文件管理系统源代码
文件管理系统
一、实验目的
通过设计一个多用户文件系统,了解操作系统中文件的组织与管理,熟悉文件管理所用的数据结构,加深对文件系统内部功能实现过程的理解。
二、实验内容
1.用C语言或C++语言设计一个最多包括N个用户的多用户文件系统,约定每个用户最多保存M个文件。
同时限制一个用户在进入系统后,最多打开L个文件。
2.系统应具备一定的健壮性。
即能够检查用户所输入命令的正确性,出错时显示出必要的信息。
另外,对文件需设置必要的保护措施。
3.文件目录结构采用两级目录结构:
主文件目录和用户文件目录
/*用户数*/
/*一个用户可保存M个文件*//*用户只能一次打开L个文件*//*主文件目录*/
#include"io.h"#include"conio.h"#include"stdio.h"#include"stdlib.h"#include"malloc.h"#include"string.h"#include"ctype.h"#defineN30#defineM20#defineL5typedefstructMFD{
charusername[100];
charpassword[100];
FILEfp;/*文件指针*/
}MFD;
///////////
typedefstructUFD/*用户文件目录*/{
charfilename[256];
charprotect;/*保护码*/
intlength;/*文件长度*/}UFD;
/*打开文件目录*/
//////////typedefstructOFD
{
charfilename[256];
charopencode;/*打开保护码*/intfp;/*读写指针*/
}OFD;
//////////typedefstructCOMM/*命令串*/
{
charstring[256];/*命令*/
structCOMM*next;/*后继指针*/}COMM;
////////////
/*主文件目录数组*/
/*用户文件目录数组*/
/*打开文件目录数组*/
/*命令串指针*/
MFDmainfd[N];
UFDuserfd[M];
OFDopenfd[L];
////////COMM*command;charusername[10];
intusernum,savenum,opennum;intworkfile;
voidinit();
voidinit_ufd(char*username);/*voidmesg(char*str);
char*getpass();
char*getuser();
COMM*readcommand();
voidlogin();
voidlogout();
voidsetpass();
voidcreate();
voidmydelete();
voidmyread();
voidmyopen();
voidmyclose();
voidmywrite();
voidhelp();
voiddir();
voidmycopy();
voidmyrename();
/////////////
初始化用户文件目录*//*消息*/
/*设置口令函数声明*/
/*设置用户函数声明*/
/*读命令串函数声明*/
/*用户登录*/
/*用户注销*/
/*设置口令*/
/*创建文件*/
/*删除文件*/
/*读文件*/
/*打开文件*/
/*关闭文件*/
/*写文件*/
/*帮助*/
/*列文件目录*/
/*复制文件*/
/*重命名文件名*/
voidmain()
{
init();
for(;;)
readcommand();
if(strcmp(command->string,"create")==0)
create();
elseif(strcmp(command->string,"delete")==0)
mydelete();
elseif(strcmp(command->string,"open")==0)myopen();
elseif(strcmp(command->string,"close")==0)myclose();
elseif(strcmp(command->string,"read")==0)myread();
elseif(strcmp(command->string,"write")==0)mywrite();
elseif(strcmp(command->string,"copy")==0)mycopy();
elseif(strcmp(command->string,"rename")==0)myrename();
elseif(strcmp(command->string,"login")==0)login();
elseif(strcmp(command->string,"setpass")==0)setpass();
elseif(strcmp(command->string,"logout")==0)logout();
elseif(strcmp(command->string,"help")==0)help();
elseif(strcmp(command->string,"dir")==0)dir();
elseif(strcmp(command->string,"exit")==0)break;
else
mesg("Badcommand!
");
}
}
///////////////////
voidinit()
{
FILE*fp;
chartempname[20],temppass[20];
inti=0;
usernum=0;
savenum=0;
opennum=0;
strcpy(username,"");if((fp=fopen("mainfile.txt","r"))!
=NULL){
while(!
feof(fp))
strcpy(tempname,"");
fgets(tempname,20,fp);
if(strcmp(tempname,"")!
=0)
{
fgets(temppass,20,fp);tempname[strlen(tempname)-1]='\0';temppass[strlen(tempname)-1]='\0';strcpy(mainfd[usernum].username,tempname);strcpy(mainfd[usernum].password,tempname);usernum++;
}
}
fclose(fp);
}
}
///////////////////////
voidinit_ufd(char*username)/*初始化用户文件目录*/
{
FILE*fp;
chartempfile[100],tempprot;
inttemplength;
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);tempfile[strlen(tempfile)-1]='\0';strcpy(userfd[savenum].filename,tempfile);userfd[savenum].protect=tempprot;userfd[savenum].length=templength;savenum++;
fgets(tempfile,50,fp);
fclose(fp);
}
////////////////////
voidmesg(char*str)
{
printf("\n%s\n",str);
}
////////////////////////////
char*getuser()
{
charusername[20];
chartemp;
inti;
username[0]='\0';
for(i=0;i<10;)
{
while(!
kbhit());
temp=getch();
if(isalnum(temp)||temp=='_'||temp==13){
username[i]=temp;if(username[i]==13)
{
username[i]='\0';break;
}
putchar(temp);
i++;
username[i]='\0';
}
}
return(username);
}
///////////////////
char*getpass()
{
charpassword[20];
chartemp;
inti;
password[0]='\0';
for(i=0;i<10;)
while(!
kbhit());
temp=getch();
if(isalnum(temp)||temp=='_'||temp==13)
{
password[i]=temp;
if(password[i]==13)
{
password[i]='\0';
break;
}
putchar('*');
i++;
password[i]='\0';
}
}
return(password);
}
///////////////
COMM*readcommand()
{
chartemp[256];
charline[256];
inti,end;
COMM*newp,*p;
command=NULL;
strcpy(line,"");while(strcmp(line,"")==0)
{
printf("\nc:
\\>");
gets(line);
}
for(i=0;i<=strlen(line);i++)
{
if(line[i]=='')
i++;
end=0;
while(line[i]!
='\0'&&line[i]!
='')
{
temp[end]=line[i];
end++;
i++;
}
if(end>0)
temp[end]='\0';
newp=(COMM*)malloc(sizeof(COMM*));
strcpy(newp->string,temp);
newp->next=NULL;
if(command==NULL)command=newp;
else
{
p=command;
while(p->next!
=NULL)p=p->next;
p->next=newp;
}
}
}p=command;
return(command);
}
/////////////////////////////
voidlogin()/*用户注册*/
{
FILE*fp;
inti;
charpassword[20],confirm[20],tempname[20];if(command->next==NULL)
{
printf("\nUserName:
");strcpy(tempname,getuser());
}
elseif(command->next->next!
=NULL)
{
mesg("Toomanyparameters!
");
return;
}
elsestrcpy(tempname,command->next->string);
for(i=0;ibreak;/*从mainfd表中查找要注册的用户*/
if(i>=usernum)/*新用户*/
{
printf("\nnewuseraccount,enteryourpasswordtwice!
");printf("\nPassword:
");
strcpy(password,getpass());/*输入口令且返回之*/printf("\nPassword:
");
strcpy(confirm,getpass());/*第二次输入口令*/if(strcmp(password,confirm)==0)/*用户数不能超过N*/{
if(usernum>=N)
mesg("Createnewaccountfalse!
numberofuserasscountlimited.\nloginfasle!
");
else
{
strcpy(mainfd[usernum].username,tempname);/*把新用户和口令填入mainfd中*/
strcpy(mainfd[usernum].password,password);usernum++;
strcpy(username,tempname);
mesg("Createanewuser!
\nloginsuccess!
");
init_ufd(username);/*初始化用户文件目录*/
fp=fopen("mainfile.txt","w+");/*把新用户填入mainfile.txt文件中*/
for(i=0;i{
fputs(mainfd[i].username,fp);fputs("\n",fp);
fputs(mainfd[i].password,fp);
fputs("\n",fp);
}
fclose(fp);
}
}
else
{
mesg("Createnewaccountfalse!
Errorpassword.");
mesg("loginfalse!
");
}
}
else
{
printf("\nPassword:
");strcpy(password,getpass());
if(strcmp(mainfd[i].password,password)!
=0)
mesg("Loginfalse!
Errorpassword.");
else
{
mesg("Loginsuccess!
");
strcpy(username,tempname);
init_ufd(username);
}
}
}
/////////////////////////
voidlogout()/*用户注销*/
{if(command->next!
=NULL)
mesg("Toomanyparameters!
");
elseif(strcmp(username,"")==0)
mesg("Nouserlogin!
");
else{strcpy(username,"");opennum=0;savenum=0;workfile=-1;mesg("Userlogout!
");
}
}
////////////////////
voidsetpass()/*修改口令*/
{
inti=0;
FILE*fp;
charoldpass[20],newpass[20],confirm[20];if(strcmp(username,"")==0)mesg("Nouserlogin!
");
else{
printf("\nOldpassword:
");strcpy(oldpass,getpass());for(inti=0;iif(strcmp(mainfd[i].username,username)==0)break;
}if(strcmp(mainfd[i].password,oldpass)!
=0)mesg("Oldpassworderror!
");
else
{
printf("\nNewpassword:
");strcpy(newpass,getpass());printf("\nConfirmpassword:
");
strcpy(confirm,getpass());if(strcmp(newpass,confirm)!
=0)
mesg("Passwordnotchange!
confirmdifferent.");
else
{strcpy(mainfd[i].password,newpass);mesg("Passwordchange!
");
fp=fopen("mainfile.txt","w+");for(i=0;i{
fputs(mainfd[i].username,fp);fputs("\n",fp);
fputs(mainfd[i].password,fp);
fputs("\n",fp);
}
fclose(fp);
}
}
}
}
////////////////
voidcreate()
{
inti=0;
FILE*fp;
if(strcmp(username,"")==0)
mesg("Nouserlgoin!
");
elseif(command->next==NULL)mesg("Toofewparametets!
");
elseif(command->next->next!
=NULL)
mesg("Toomanyparameters!
");
else
{
for(i=0;i{
if(strcmp(userfd[i].filename,command->next->string)==0)break;
}
if(imesg("Error!
thefilealreadyexisted!
");
elseif(savenum>=M)
mesg("Error!
connotcreatefile!
numberoffileslimited!
");else
strcpy(userfd[savenum].filename,command->next->string);userfd[i].protect='r';
userfd[i].length=rand();
savenum++;mesg("Createfilesuccess!
");fp=fopen(username,"w+");for(i=0;i{fputs(userfd[i].filename,fp);fputs("\n",fp);fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].length);
}fclose(fp);
}
}
}
/////////////////
voidmydelete()/*删除*/
{
inti=0;
inttempsave=0;
FILE*fp;if(strcmp(username,"")==0)mesg("Nouserlgoin!
");
elseif(command->next==NULL)mesg("Toofewparametets!
");
elseif(command->next->next!
=NULL)mesg("Toomanyparameters!
");
else{for(i=0;iif(strcmp(userfd[i].filename,command->next->string)==0)break;
}
if(i>=savenum)
mesg("Error!
thefilenotexisted!
");else
{tempsave=i;for(i=0;iif(strcmp(command->next->string,openfd[i].filename)==0)break;
}if(i>=opennum)mesg("Filenotopen");
////////////////////////////////////////////
else
{if(tempsave==savenum-1)savenum--;
else
{for(;tempsave{strcpy(userfd[tempsave].filename,userfd[tempsave+1].filename);userfd[tempsave].protect=userfd[tempsave+1].protect;userfd[tempsave].length=userfd[tempsave+1].length;
}
savenum--;
}mesg("Deletefilesuccess!
");fp=fopen(username,"w+");
for(i=0;i{fputs(userfd[i].filename,fp);fputs("\n",fp);fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].length);
}fclose(fp);
}
}
}
}
//////////////////
voidmyread()/*读*/
{
inti=0;
inttempsave=0;
chartempfile[100];if(strcmp(username,"")==0)mesg("Nouserlgoin!
");
elseif(command->next==NULL)
mesg("Toofewparametets!
");
elseif(command->next->next!
=NULL)
mesg("Toomanyparameters!
");
else
{
strcpy(tempfile,command->next->string);for(i=0;i{if(strcmp(tempfile,userfd[i].filename)==0)