-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_backend.c
More file actions
120 lines (102 loc) · 2.99 KB
/
Copy pathuser_backend.c
File metadata and controls
120 lines (102 loc) · 2.99 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define USERS_FILE "users.txt"
#define KEY 5
typedef struct User {
char username[50];
char password[50];
char address[500];
struct User* next;
} User;
User* userList = NULL;
void encryptPassword(char* password) {
for (int i = 0; password[i] != '\0'; i++) {
password[i] ^= KEY;
}
}
void decryptPassword(char* password) {
for (int i = 0; password[i] != '\0'; i++) {
password[i] ^= KEY;
}
}
void loadUsers() {
FILE* file = fopen(USERS_FILE, "r");
if (!file) return;
char username[50], password[50], address[500];
while (fscanf(file, "%s %s %[^\n]", username, password, address) != EOF) {
User* newUser = (User*)malloc(sizeof(User));
strcpy(newUser->username, username);
strcpy(newUser->password, password);
decryptPassword(newUser->password);
strcpy(newUser->address, address);
newUser->next = userList;
userList = newUser;
}
fclose(file);
}
void saveUsers() {
FILE* file = fopen(USERS_FILE, "w");
if (!file) return;
User* current = userList;
while (current) {
encryptPassword(current->password);
fprintf(file, "%s %s %s\n", current->username, current->password, current->address);
decryptPassword(current->password);
current = current->next;
}
fclose(file);
}
User* findUser(const char* username) {
User* current = userList;
while (current) {
if (strcmp(current->username, username) == 0)
return current;
current = current->next;
}
return NULL;
}
void updateAddress(const char* username, const char* newAddress) {
User* user = findUser(username);
if (user) {
strcpy(user->address, newAddress);
saveUsers();
printf("? Address updated for %s!\n", username);
} else {
printf("? User not found!\n");
}
}
void showAddress(const char* username) {
User* user = findUser(username);
if (user) {
printf("?? Address for %s: %s\n", username, user->address);
} else {
printf("? User not found!\n");
}
}
void menu() {
int ch;
char username[50], address[500];
printf("Enter your username: ");
fgets(username, sizeof(username), stdin);
username[strcspn(username, "\n")] = 0;
do {
printf("\n--- USER DASHBOARD: %s ---\n", username);
printf("1. View Saved Address\n2. Update Address\n3. Exit\nChoice: ");
scanf("%d", &ch); getchar();
if (ch == 1) {
showAddress(username);
} else if (ch == 2) {
printf("Enter new address:\n");
fgets(address, sizeof(address), stdin);
address[strcspn(address, "\n")] = 0;
updateAddress(username, address);
}
} while (ch != 3);
}
int main() {
loadUsers();
menu();
return 0;
}