forked from kkim-hgu/manager2019
-
Notifications
You must be signed in to change notification settings - Fork 26
/
user.c
84 lines (79 loc) · 1.78 KB
/
user.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "user.h"
int load_file(LOGIN* list[], char* filename){
int count=0;
FILE *datafile = fopen(filename, "r");
while(!feof(datafile)){
list[count]=(LOGIN*)malloc(sizeof(LOGIN));
fscanf(datafile,"%s %s",list[count]->id,list[count]->password);
count++;
}
printf("%d records read!\n",count);\
fclose(datafile);
return count;
}
void join(LOGIN* list[], int count){
char id[20], pass[20];
while(1){
printf("Enter new user id >> ");
scanf("%s", id);
int dup=0;
for(int i=0;i<count;i++){
if(strcmp(id, list[i]->id)==0) {
dup=1; break;
}
}
if(dup==1){
printf("Already exist!!\n");
}
else{
printf("Enter password >> ");
scanf("%s", pass);
list[count] = (LOGIN*)malloc(sizeof(LOGIN));
strcpy(list[count]->id, id);
strcpy(list[count]->password, pass);
printf("New user added!\n");
break;
}
}
}
int login(LOGIN* list[], int count){
char id[20], pass[20];
printf("Enter user id >> ");
scanf("%s", id);
int dup=0, found;
for(int i=0;i<count;i++){
if(strcmp(id, list[i]->id)==0) {
dup=1;
found = i;
break;
}
}
if(dup!=1){
printf("No such user!!\n");
return -1;
}
else{
printf("Enter password >> ");
scanf("%s", pass);
if(strcmp(list[found]->password, pass)==0){
printf("Welcome %s!!\n", id);
return 1;
}
else{
printf("Password incorrect!!\n");
return 0;
}
}
}
void logout(int* is_login){
*is_login = 0;
printf("Logout!!\n");
}
void save_file(LOGIN* list[], int count, char* filename){
FILE *datafile = fopen(filename, "w");
for(int i=0; i<count; i++){
fprintf(datafile, "%s %s\n", list[i]->id, list[i]->password);
}
printf("%s Saved!\n", filename);
fclose(datafile);
}