第六次作业
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct sysuser
{
char username[20];
char password[16];
};
int zhuce();
int CheckUserValid(struct sysuser *psu);
int main()
{
printf("请注册账号\n");
if(zhuce)
printf("注册成功\n");
struct sysuser su;
printf("输入用户名\n");
scanf("%s",su.username);
printf("输入密码\n");
scanf("%s",su.password);
if(CheckUserValid(&su)==1)
printf("登陆成功\n");
else
printf("账号或密码不正确\n");
}
int zhuce()//注册函数
{
FILE *fp;
struct sysuser su;//文件指针
if((fp=fopen("user.txt","w"))==NULL)
{
printf("抱歉,没有指定文件");
exit(0);
}
printf("请输入用户名\n");
scanf("%s",su.username);
printf("请输入密码\n");
scanf("%s",su.password);
fprintf(fp,"%s %s\n",su.username,su.password);
fclose(fp);
return 1;
}
int CheckUserValid(struct sysuser *psu)//登录函数
{
FILE *fp;
if((fp=fopen("user.txt","r"))==NULL)
{
printf("找不到指定文件");
exit(0);
}
char use[32],rightUse[32],pass[16];
int flag=0;
strcpy(use,(*psu).username);
strcpy(pass,(*psu).password);
strcat(use," ");
strcat(use,pass);
strcat(use,"\n");
while(!feof(fp))
{
fgets(rightUse,32,fp);
if(strcmp(use,rightUse)==0)
{
flag=1;
break;
}
if(fclose(fp))
{
printf("不能关闭这个文件");
exit(0) ;
}
}
return flag;
}