📄 1.txt
字号:
#include <io.h>
#include <conio.h> //里面有clrscr()清屏函数
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h> //内存分配函数
#include <string.h>
#include <ctype.h>
#define N 30 //用户数
#define M 20 //一个用户可保存M个文件
#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 length; //文件长度
}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[100];
int usernum,savenum,opennum;
int workfile;
void init(); //初始化主文件目录数组
void init_ufd(char *username); //初始化用户文件目录
void mesg(char *str); //输出函数
char *getuser(); //设置用户函数声明
char *getpass(); //设置口令函数声明
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(); //文件改名
void main()
{
init();
while(1)
{
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!");
}
}
void mesg(char *str)
{
printf("\n %s\n",str);
}
void init() //初始化主文件目录数组
{
FILE *fp; //文件指针
char tempname[20],temppass[20];
usernum=0; //全局变量初始化
savenum=0;
opennum=0;
strcpy(username,"");
//用户使用时,建立一个mainfile.txt文件,包括每个用户的用户名和口令
//然后,才能运行此程序
if((fp=fopen("mainfile.txt","r"))!=NULL)//以读方式打开文件mainfile.txt
{
while(!feof(fp))//若不是文件尾
{
strcpy(tempname,""); //清空数组操作
fgets(tempname,20,fp); //读用户名
if(strcmp(tempname,"")!=0)
{
fgets(temppass,20,fp);
tempname[strlen(tempname)-1]='\0'; //设置串结束符
temppass[strlen(temppass)-1]='\0';
strcpy(mainfd[usernum].username,tempname); //生成mainfd数组
strcpy(mainfd[usernum].password,temppass); //生成userfd数组
usernum++; //生成usernum的值
}
if(usernum>=N)
break;
}
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,"r+"))!=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);
}
}
}
}
char *getuser() //设置用户函数声明
{
char username[20];
char temp;
int i;
username[0]='\0';
for(i=0;i<20;)
{
while(!kbhit()); //按用户名规则输入用户名
temp=getch();
if(isalnum(temp)||temp=='_'||temp==(char)13)
{
username[i]=temp;
if(username[i]==(char)13)
{
username[i]='\0';
break;
}
putchar(temp);
i++;
username[i]='\0';
}
}
return username;
}
char *getpass() //设置口令函数声明
{
char password[20];
char temp;
int i;
password[0]='\0';
for(i=0;i<20;)
{
while(!kbhit()); //等待按键
temp=getch();
if(isalnum(temp)||temp=='_'||temp==(char)13)
{
password[i]=temp;
if(password[i]==(char)13)
{
password[i]='\0';
break;
}
putchar('*');
i++;
password[i]='\0';
}
}
return password;
}
COMM *readcommand() //读命令串函数声明
{
char temp[256];
char line[256];
unsigned int i,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;
}
void login() //用户注册
{
FILE *fp;
int i;
char password[20],confirm[20],tempname[20];
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);
for(i=0;i<usernum;i++)
if(strcmp(mainfd[i].username,tempname)==0)
break; //从mainfd表中查找要注册的用户
if(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) //两次输入的口令是否相同的处理情况
{
if(usernum>=N) //用户数不能超过N
mesg("Creat new account fasle!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("Creat a new user!\n login success!");
init_ufd(username); //初始化用户文件目录
fp=fopen("mainfile.txt","w+"); //把新用户填入mainfile.txt文件中
for(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("Creat new account false!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 setpass() //重新设置口令
{
int i;
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(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 changed! cinfirm different.");
else
{
strcpy(mainfd[i].password,newpass);
mesg("Password changed!");
fp=fopen("mainfile.txt","w+");
for(i=0;i<usernum;i++) //写回到mainfile.txt
{
fputs(mainfd[i].username,fp);
fputs("\n",fp);
fputs(mainfd[i].password,fp);
fputs("\n",fp);
}
fclose(fp);
}
}
}
}
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 logout!");
}
}
}
void create() //创建用户文件
{
int i;
FILE *fp;
if(strcmp(username,"")==0)
mesg("No user login!");
else
{
if(command->next==NULL)
mesg("Too few parameters!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
{
for(i=0;i<savenum;i++)
if(strcmp(userfd[i].filename,command->next->string)==0)
break;
if(i<savenum) //文件已经存在的情况
mesg("Error! the file already existed!");
else if(savenum>=M) //保存的文件数目超过M的处理情况
mesg("Error! cannot create file! nimber of files limited!");
else
{
strcpy(userfd[savenum].filename,command->next->string);
userfd[savenum].protect='r';
userfd[savenum].length=rand();
savenum++;
mesg("Create file success!");
fp=fopen(username,"w+"); //写回到文件username
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].length);
}
fclose(fp);
}
}
}
}
void myopen() //打开文件
{
int i;
int type=0;
char tempcode;
char tempfile[100];
if(strcmp(username,"")==0)
mesg("No user login!");
else
if(command->next==NULL)
mesg("Too few parameters!");
else //存在2个或3个参数的处理
{
strcpy(tempfile,"");
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->string[0]=='/')
mesg("Error! /r /w /d request!");
else if(command->next->next!=NULL)
mesg("Too many parameters!");
else
strcpy(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);
else mesg("Too few parameters!");
if(strcmp(tempfile,"")!=0)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -