forked from jerry10004/manager2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.c
127 lines (111 loc) · 2.61 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "user.h"
void list_file(LOGIN* list[], int count){
#ifdef DEBUG_MODE
printf("DEBUG>> datafile opened! \\n");
#endif
printf("User list (id/password)\n");
for(int i=0; i<count; i++)
printf("[%d] %s / %s\n",i+1,list[i]->id,list[i]->password);
}
int load_file(LOGIN* list[], char* filename){
int count=0;
FILE *datafile = fopen(filename, "r");
int yn;
if(datafile == NULL){
printf("%s file not exist! make anyway? (Yes 1, No 2) >> ",filename);
scanf("%d",&yn);
if(yn == 1)
datafile = fopen(filename,"w");
printf("Welcome!!\n");
}
else{
while(!feof(datafile)){
list[count]=(LOGIN*)malloc(sizeof(LOGIN));
fscanf(datafile,"%s %s",list[count]->id,list[count]->password);
count++;
}
printf("Welcome!! ");
printf("%d records read!\n",count);\
fclose(datafile);
return count;
}
fclose(datafile);
}
void join(LOGIN* list[], int count){
#ifdef DEBUG_MODE
printf("DEBUG>> datafile opened! \\n");
#endif
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){
#ifdef DEBUG_MODE
printf("DEBUG>> datafile opened! \\n");
#endif
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){
#ifdef DEBUG_MODE
printf("DEBUG>> datafile opened! \\n");
#endif
*is_login = 0;
printf("Bye!!\n");
}
void save_file(LOGIN* list[], int count, char* filename){
#ifdef DEBUG_MODE
printf("DEBUG>> datafile opened! \\n");
#endif
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);
}