自己做的简单注册登入系统

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <windows.h>
  4 #include <conio.h>
  5 
  6 void enter();
  7 int main(){
  8 
  9     enter();
 10     return 0;
 11 }
 12 void enter(){
 13     int i = 0;
 14     char keyboard;
 15     FILE *file1;
 16     FILE *file2;
 17     FILE *file3;
 18     FILE *file4;
 19     char *ret;
 20     char user[256];
 21     char line[256];
 22     char name[50];
 23     char password[50];
 24     char name_right[50];
 25     char password_right[50];
 26 
 27     printf("欢迎使用本产品\n");
 28     printf("1、老用户登入\n");
 29     printf("2、新用户注册\n");
 30 
 31     keyboard = getch();
 32     switch (keyboard){
 33     case '1':  // 登入老号
 34         while (1){
 35             file1 = fopen("user.txt", "r");
 36             if (!file1){
 37                 printf("系统错误");
 38             }
 39             printf("请输入账号:");
 40             scanf("%s", name);
 41             printf("请输入密码:");
 42             scanf("%s", password);
 43             while (1){
 44                 ret = fgets(line, sizeof(line), file1);
 45                 if (!ret){
 46                     break;
 47                 }
 48                 sscanf(line, "%s %s", name_right, password_right);
 49                 if (!strcmp(name, name_right) && !strcmp(password, password_right)){
 50                     break;
 51                 }
 52             }
 53             if (ret){
 54                 break;
 55             }
 56             else{
 57                 printf("账号或密码错误!\n");
 58                 printf("登入失败!\n");
 59             }
 60         }
 61         printf("登入成功!\n");
 62         fclose(file1);
 63         break;
 64     case '2':    //开创新号
 65         while (1){
 66             file1 = fopen("creat.txt", "w+");
 67             if (!file1){
 68                 printf("系统错误!");
 69             }
 70             file2 = fopen("test.txt", "w+");
 71             if (!file2){
 72                 printf("系统错误!");
 73             }
 74             //如果已经存在用户名则一直创建用户名
 75             while (1){
 76                 file4 = fopen("user.txt", "r");
 77                 if (!file4){
 78                     printf("系统错误!");
 79                 }
 80                 printf("创建账号:");
 81                 scanf("%s", name);
 82                 while (1){
 83                     ret = fgets(line, sizeof(line), file4);
 84                     if (!ret){
 85                         break;
 86                     }
 87                     sscanf(line, "%s", name_right);
 88                     if (!strcmp(name, name_right)){
 89                         break;
 90                     }
 91                 }
 92                 if (ret){
 93                     printf("账号已存在!\n");
 94                 }
 95                     else{
 96                         break;
 97                     }
 98             }
 99 
100             printf("请输入密码:");
101             scanf("%s", password);
102             //验证两次密码是否一致
103             printf("请重新输入密码:");
104             scanf("%s", password_right);
105             fputs(name, file1);
106             fputs(password, file1);
107             fputs(password_right, file2);
108             //如果创建成功则把账号密码放在一个文本中
109             if (!strcmp(password, password_right)){            
110                 file3 = fopen("user.txt", "a+");
111                 if (!file3){
112                     printf("系统错误!");
113                 }    
114                 fputs(name, file3);
115                 fputc(32, file3);
116                 fputs(password, file3);
117                 fputc(10, file3);
118                 printf("创建成功!");
119                 break;
120             }
121             else{
122                 printf("创建失败!\n");
123             }
124         }
125         fclose(file1);
126         fclose(file2);
127         fclose(file3);
128         fclose(file4);
129         break;
130     }
131 
132 }

 

posted @ 2018-08-26 23:38  浪上了天  阅读(286)  评论(0编辑  收藏  举报