📄 fms.cpp
字号:
// FMS.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "io.h"
#include "conio.h"
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#include "string.h"
#include "ctype.h"
#define M 20/*一个用户可保存M个文件*/
#define N 30/*用户数*/
#define L 5/*用户只能一次打开L个文件*/
typedef struct MFD/*主文件目录*/
{
char username[100];
char password[100];
FILE fp;/*文件指针*/
}MFD;
typedef struct UFD/*用户文件目录*/
{
char filename[256];
char protect;/*保护码*/
int lenght;/*文件长度*/
}UFD;
typedef struct OFD/*打开文件目录*/
{
char filename[256];
char opencode;/*打开保护码*/
int fp;/*读写指针*/
}OFD;
typedef struct COMM/*命令串*/
{
char string[256];/*命令*/
struct COMM *next;/*后继指针*/
}COMM;
MFD mainfd[N];/*主文件目录数组*/
UFD userfd[M];/*用户文件目录数组*/
OFD openfd[L];/*打开文件目录数组*/
COMM *command;/*命令传指针*/
char username[10];
int usernum=0,savenum=0,opennum=0;
int workfile=0;
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 myopen();/*打开*/
void myclose();/*关闭*/
void mywrite();/*写*/
void help();/*帮助*/
void dir();/*列文件目录*/
void mycopy();/*复制*/
void myrename();/*文件改名*/
int main(int argc, char* argv[])
{
init();
for(;;)
{
readcommand();
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")==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")==0)
help();
else if(strcmp(command->string,"dir")==0)
dir();
else if(strcmp(command->string,"exit")==0)
break;
else
mesg("Bad command");
}
return 0;
}
void init()
{
FILE *fp;
char tempname[20],tempass[20];
int i=0;
usernum=0;
savenum=0;
opennum=0;
strcpy(username,"");
/*用户使用时,建立一个mainfile.txt文件,包括每个用户的用户名和口令*/
/*然后才能运行此程序*/
if((fp=fopen("mainfile.txt","r"))!=NULL)
{
while(!feof(fp))
{
strcpy(tempname,"");
fgets(tempname,20,fp);
if(strcmp(tempname,"")!=0)
{
fgets(tempass,20,fp);
tempname[strlen(tempname)-1]='\0';
tempass[strlen(tempass)-1]='\0';
strcpy(mainfd[usernum].username,tempname);
strcpy(mainfd[usernum].password,tempass);
usernum++;
}
}
fclose(fp);
}
}
void init_ufd(char *username)/*初始化用户文件目录*/
{
FILE *fp;
char tempfile[100],tempprot;
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);
tempfile[strlen(tempfile)-1]='\0';
strcpy(userfd[savenum].filename,tempfile);
userfd[savenum].protect=tempprot;
userfd[savenum].lenght=templength;
savenum++;
fgets(tempfile,50,fp);
}
}
}
fclose(fp);
}
void mesg(char *str)
{
printf("\n %s\n",str);
}
char *getpass()/*设置密码*/
{
char password[20];
char temp;
int i=0;
password[0]='\0';
for(;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);
}
char *getuser()/*设置用户名*/
{
char username[20];
char temp;
int i=0;
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);
}
COMM *readcommand()/*读命令串*/
{
char temp[256];
char line[256];
int i=0,end=0;
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;
}
void login()/*用户登录*/
{
FILE *fp;
int i=0;
char password[20],confirm[20],tempname[20];
COMM *p=command;
while(p!=NULL)
{
printf("%s\n",p->string);
p=p->next;
}
if(command->next==NULL)
{
printf("\n User Name:");
strcpy(tempname,getuser());
}
else if(command->next->next!=NULL)
{
mesg("Too many parameters");
return;
}
else
strcpy(tempname,command->next->string);
// printf("%s%d\n",tempname,usernum);
for(i=0;i<usernum;i++)
{
if(strcmp(mainfd[i].username,tempname)==0)
break;
}
if(i>=usernum)
{
printf("\n new user account,enter your password twice!");
printf("\n Password:");
strcpy(password,getpass());
printf("\n confirm Password:");
strcpy(confirm,getpass());
if(strcmp(password,confirm)==0)
{
if(usernum>=N)
mesg("Create new account false! number of user account limited.\n login false!");
else
{
strcpy(mainfd[usernum].username,tempname);//把新用户和口令填如mainfd中
strcpy(mainfd[usernum].password,password);
usernum++;
strcpy(username,tempname);
mesg("Create a new user! \n login suc!cess");
init_ufd(username);
fp=fopen("mainfile.txt","w+");//将新用户添加到mainfile.txt文件中
for(int i=0;i<usernum;i++)
{
fputs(mainfd[i].username,fp);
fputs("\n",fp);
fputs(mainfd[i].password,fp);
fputs("\n",fp);
}
fclose(fp);
}
}
else
{
mesg("Create new account fasle! Error password.");
mesg("login false!");
}
}
else
{
printf("\n Password:");
strcpy(password,getpass());
if(strcmp(mainfd[i].password,password)!=0)
mesg("Login false! Error password.");
else
{
mesg("Login success!");
strcpy(username,tempname);
init_ufd(username);
}
}
}
void logout()/*用户注销*/
{
if(command->next!=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 login!");
}
}
void setpass()/*修改口令*/
{
int i=0;
FILE *fp;
char oldpass[20],newpass[20],confirm[20];
if(strcmp(username,"")==0)
mesg("No user login!");
else
{
printf("\n Old password:");
strcpy(oldpass,getpass());
for(int i=0;i<usernum;i++)
{
if(strcmp(mainfd[i].username,username)==0)
break;
}
if(strcmp(mainfd[i].password,oldpass)!=0)
mesg("Old password error!");
else
{
printf("\n New password:");
strcpy(newpass,getpass());
printf("\n Confirm password:");
strcpy(confirm,getpass());
if(strcmp(newpass,confirm)!=0)
mesg("Password not change! confirm different.");
else
{
strcpy(mainfd[i].password,newpass);
mesg(" Password change !");
fp=fopen("mainfile.txt","w+");
for(i=0;i<usernum;i++)
{
fputs(mainfd[i].username,fp);
fputs("\n",fp);
fputs(mainfd[i].password,fp);
fputs("\n",fp);
}
fclose(fp);
}
}
}
}
void create()
{
int i=0;
FILE *fp;
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
{
for(i=0;i<savenum;i++)
{
// printf("%s %d %s",userfd[i].filename,i,command->next->string);
if(strcmp(userfd[i].filename,command->next->string)==0)
break;
}
if(i<savenum)
mesg("Error! the file already existed!");
else if(savenum>=M)
mesg("Error! connot create file! number of files limited!");
else
{
strcpy(userfd[savenum].filename,command->next->string);
userfd[i].protect='r';
userfd[i].lenght=rand();
savenum++;
mesg("Create file success!");
fp=fopen(username,"w+");
for(i=0;i<savenum;i++)
{
fputs(userfd[i].filename,fp);
fputs("\n",fp);
fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].lenght);
}
fclose(fp);
}
}
}
void mydelete()/*删除*/
{
int i=0;
int tempsave=0;
FILE *fp;
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
{
for(i=0;i<savenum;i++)
{
// printf("%s %d %s",userfd[i].filename,i,command->next->string);
if(strcmp(userfd[i].filename,command->next->string)==0)
break;
}
if(i>=savenum)
mesg("Error! the file not existed!");
else
{
tempsave=i;
for(i=0;i<opennum;i++)
{
if(strcmp(command->next->string,openfd[i].filename)==0)
break;
}
if(i>=opennum)
mesg("File not open");
////////////////////////////////////////////
else
{
if(tempsave==savenum-1)
savenum--;
else
{
for(;tempsave<savenum-1;tempsave++)
{
strcpy(userfd[tempsave].filename,userfd[tempsave+1].filename);
userfd[tempsave].protect=userfd[tempsave+1].protect;
userfd[tempsave].lenght=userfd[tempsave+1].lenght;
}
savenum--;
}
mesg("Delete file success!");
fp=fopen(username,"w+");
for(i=0;i<savenum;i++)
{
fputs(userfd[i].filename,fp);
fputs("\n",fp);
fprintf(fp,"%c\n%d\n",userfd[i].protect,userfd[i].lenght);
}
fclose(fp);
}
}
}
}
void myread()/*读*/
{
int i=0;
int tempsave=0;
char tempfile[100];
if(strcmp(username,"")==0)
mesg("No user lgoin!");
else if(command->next==NULL)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -