-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
70 lines (56 loc) · 1.55 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
// user.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "user.h"
struct User {
char username[50];
char password[50];
};
void saveUser(struct User user) {
FILE *file = fopen("users.txt", "a");
if (file != NULL) {
fprintf(file, "%s %s\n", user.username, user.password);
fclose(file);
} else {
printf("Error: Unable to save user data.\n");
}
}
void userRegistration() {
struct User user;
printf("Enter user username: ");
scanf("%s", user.username);
printf("Enter user password: ");
scanf("%s", user.password);
saveUser(user);
printf("User registration successful!\n");
}
int verifyUser(struct User inputUser) {
struct User storedUser;
FILE *file = fopen("users.txt", "r");
if (file != NULL) {
while (fscanf(file, "%s %s", storedUser.username, storedUser.password) != EOF) {
if (strcmp(storedUser.username, inputUser.username) == 0 &&
strcmp(storedUser.password, inputUser.password) == 0) {
fclose(file);
return 1; // Credentials match
}
}
fclose(file);
}
return 0; // Credentials do not match
}
int userLogin() {
struct User user;
printf("Enter user username: ");
scanf("%s", user.username);
printf("Enter user password: ");
scanf("%s", user.password);
if (verifyUser(user)) {
printf("User login successful!\n");
return 1;
} else {
printf("Invalid credentials. User login failed.\n");
return 0;
}
}