C语言用户注册和登录模块(文件版)
实例说明:
C语言,用户注册和登录模块(文件版)
实例代码:
#include <stdio.h> #include <conio.h> #include <windows.h> #define ENTER 13 #define TAB 9 #define BKSPC 8 struct User { char fullName[50]; char email[50]; char passWord[50]; char userName[50]; char phone[11]; }; void take_input(char ch[]) { fgets(ch, 50, stdin); ch[strlen(ch) - 1] = 0; } void generte_username(char email[], char username[]) { //username[strlen(username)] ="\0"; for (int i = 0; i < strlen(email); i++) { if (email[i] == '@') break; else username[i] = email[i]; } } void take_password(char pwd[50]) { int i = 0; char ch; while (1) { ch = getch(); if (ch == ENTER || ch == TAB) { pwd[i] = '\0'; break; } else if (ch == BKSPC) { if (i > 0) { i--; printf("\b \b"); } } else { printf("*"); pwd[i++] = ch; } } } int main() { system("color 0b"); FILE *fp; int opt; struct User user; char password2[50] = {0}; printf("\n\t\t\t\t----------Welcome to Student System----------"); printf("\nPlease choose your opertaion:"); printf("\n1.Signup"); printf("\n2.Login"); printf("\n3.Exit"); printf("\n\nYour choice:\t"); scanf("%d", &opt); fgetc(stdin); switch (opt) { case 1:printf("\nEnter your name:\t"); take_input(user.fullName); printf("Enter your email:\t"); take_input(user.email); printf("Enter your contact number:\t"); take_input(user.phone); printf("Enter your password:\t"); take_password(user.passWord); printf("\nConfirm your password:\t"); take_password(password2); if (!strcmp(user.passWord, password2)) { generte_username(user.email, user.userName); fp = fopen("User.dat", "a+"); fwrite(&user, sizeof(struct User), 1, fp); if (fwrite != 0) { printf("\n\nUser resgistration success!"); } else { printf("\n\nsorry,Something went wron!"); } } else { printf("\nTwo passwords don't match!"); } break; case 2:char un[50] = {0}; char pwd[50] = {0}; struct User uer; int userfonud = 0; printf("\n\nEnter your username:\t"); take_input(un); printf("\n\nEnter your password:\t"); take_password(pwd); fp = fopen("User.dat", "r"); while (fread(&uer, sizeof(struct User), 1, fp)) { if (!strcmp(uer.userName, un)) { if (!strcmp(uer.passWord, pwd)) { printf("\n\t\t\t\t Welcome %s", uer.fullName); printf("\n\nFull Name:\t%s", uer.fullName); printf("\n\nEmail:\t%s", uer.email); printf("\n\nUsername:\t%s", uer.userName); printf("\n\nContact numeber:\t%s", uer.phone); } else { printf("\n\n password wron"); } userfonud = 1; } } if (!userfonud) { printf("\n\n User is not register"); } break; default:printf("\n\nPlease choose your opertaion:"); break; } system("pause"); return 0; }