?? 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 + -