编辑器:VS2022
由txt文件作为用户的账号和密码的数据库,实现注册账户后下次运行可以再次登录
环境搭建
账户在注册或登录时,数据储存到结构体person,注册或登录会将库txt文件中的数据读取到结构体people中与用户输入的数据匹配,账号或密码正确完成登录和账号不重复完成注册。
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
#include<string.h>
#define N 15 //控制账号密码的字符串长度
int m=0;//选项
typedef struct message{
char account[N];
char password[N];
}person;
int len=sizeof(message);
typedef struct k{
char account[N];
char password[N];
}people[10];
int th = sizeof(k);
菜单功能
void menu()
{
printf("----欢迎进入xx系统----\n");
printf("1.登录账户 2.注册账户\n");
printf("0.退出\n");
printf("----------------------\n");
}
登录系统
需要创建txt文件储存数据
void load()
{
k people[N];
message person;
FILE* rp;
if ((rp = fopen("数据库", "rb+")) == NULL)
exit(0);
fseek(rp, 0, SEEK_END);
int size=ftell(rp);
rewind(rp);
if (size != 0)
{
while (1)
{
int i=0;
printf("请输入账号:");
scanf("%s", &person.account);
printf("请输入密码:");
scanf("%s", &person.password);
while (fread(&people[0], th, 1, rp) == 1)//读取信息
if (strcmp(people[i].account, person.account) == 0)
{
printf("登录成功!\n");
fclose(rp);
return;
}
printf("登录不成功!\n");
rewind(rp);//登录不成功,把文件指针移回文件开头
}
}
}
注册系统
需要创建txt文件储存数据
void register_()
{
message person;
k people[N];
int flag = 1;
FILE *fp;//读入指针
if ((fp = fopen("数据库", "ab+")) == NULL)
exit(0);
FILE* rp;//读取指针
if ((rp = fopen("数据库", "rb+")) == NULL)
exit(0);
while (flag)
{
fseek(rp, 0, SEEK_END);
int size = ftell(rp);
rewind(rp);
if (size!= 0)
{
while (flag)
{
int i = 0;
printf("请设置账号:");
scanf("%s", &person.account);
printf("请设置密码:");
scanf("%s", &person.password);
while (fread(&people[0], th, 1, rp) == 1)//读取信息
{
}
for (i = 0; i < N; i++)//逐个比较
{
if (strcmp(person.account, people[i].account) == 0)
{
printf("注册不成功!\n");
memset(&person, 0, len);
flag = 2;
rewind(rp);
break;
}
flag = 1;
}
if (flag == 2)
continue;
fwrite(&person, len, 1, fp);
flag = 0;
printf("注册成功!\n");
}
}
fwrite(&person, len, 1, fp);
printf("注册成功!\n");
flag = 0;
}
fclose(rp);
fclose(fp);
}
主函数
用来实现程序的逻辑
int main()
{
do
{
menu();
printf("请选择选项:");
scanf("%d", &m);
switch (m)
{
case 1:load(); break;
case 2:register_(); break;
case 3:exit(0); break;
default: printf("输入错误"); break;
}
system("pause");
} while (1);
return 0;
}
源代码
#include<stdio.h>
#include<stdlib.h>
#include<stdlib.h>
#include<string.h>
#define N 15
#define M 15
int m=0;//选项
typedef struct message{
char account[N];
char password[M];
}person;
int len=sizeof(message);
typedef struct k{
char account[N];
char password[M];
}people[10];
int th = sizeof(k);
void menu()
{
printf("----欢迎进入xx系统----\n");
printf("1.登录账户 2.注册账户\n");
printf("0.退出\n");
printf("----------------------\n");
}
void load()
{
k people[N];
message person;
FILE* rp;
if ((rp = fopen("数据库.txt", "rb+")) == NULL)
exit(0);
fseek(rp, 0, SEEK_END);
int size=ftell(rp);
rewind(rp);
if (size != 0)
{
while (1)
{
int i=0;
printf("请输入账号:");
scanf("%s", &person.account);
printf("请输入密码:");
scanf("%s", &person.password);
while (fread(&people[0], th, 1, rp) == 1)//读取信息
if (strcmp(people[i].account, person.account) == 0)
{
printf("登录成功!\n");
fclose(rp);
return;
}
printf("登录不成功!\n");
rewind(rp);//登录不成功,把文件指针移回文件开头
}
}
}
void register_()
{
message person;
k people[N];
int flag = 1;
FILE *fp;//读入指针
if ((fp = fopen("数据库.txt", "ab+")) == NULL)
exit(0);
FILE* rp;//读取指针
if ((rp = fopen("数据库.txt", "rb+")) == NULL)
exit(0);
while (flag)
{
fseek(rp, 0, SEEK_END);
int size = ftell(rp);
rewind(rp);
if (size!= 0)
{
while (flag)
{
int i = 0;
printf("请设置账号:");
scanf("%s", &person.account);
printf("请设置密码:");
scanf("%s", &person.password);
while (fread(&people[0], th, 1, rp) == 1)//读取信息
{
}
for (i = 0; i < N; i++)//逐个比较
{
if (strcmp(person.account, people[i].account) == 0)
{
printf("注册不成功!\n");
memset(&person, 0, len);
flag = 2;
rewind(rp);
break;
}
flag = 1;
}
if (flag == 2)
continue;
fwrite(&person, len, 1, fp);
flag = 0;
printf("注册成功!\n");
}
}
fwrite(&person, len, 1, fp);
printf("注册成功!\n");
flag = 0;
}
fclose(rp);
fclose(fp);
}
int main()
{
do
{
menu();
printf("请选择选项:");
scanf("%d", &m);
switch (m)
{
case 1:load(); break;
case 2:register_(); break;
case 3:exit(0); break;
default: printf("输入错误"); break;
}
system("pause");
} while (1);
return 0;
}