Member • Jun 17, 2012
User Login and Registration using Files in C
Hello Everyone!! i'm working on this C program which deals with user registration and login page. I was successful is writing the program for only 1 user. But now when i'm trying it for 100 users, i get a few problems that are:
1. The program starts with registering 1st user records in w[0], it works fine if i login at this time.. But, when I close the program and restart it to make the second registration, again it starts with w[0] which copies the same username and password as the 1st registration.. How to overcome this problem???
2. if I take just w instead of w[99] and make the necessary adjustments in the program, I'm able to get 2 different username's and passwords in the file but at the time of login, if program is able to read any of the username and password, it logs in..
ex: suppose there are two username's and passwords (username followed by the password) as,
abc pass abc1 pass1
now, at the time of log in, if user enters username as abc and password as pass1, the program must not log in.. but it does.
NEED SUGGESTIONS!!
below, is the C code i've written.
I'm facing problem in void reg() and void login()
#include
#include
#include
static int i=0;
struct web
{
char name[30],pass[30];
}w[99];
int n;
void login(void);
void reg(void);
int main()
{
clrscr();
printf("
WELCOME TO MY WEBSITE");
printf("
=====================");
printf("
Press Enter to proceed...!!");
if(getch()==13)
clrscr();
XY:
printf("
1. LOGIN 2. REGISTER");
printf("
ENTER YOUR CHOICE: ");
scanf("%d",&n);
switch(n)
{
case 1: clrscr();
login();
break;
case 2: clrscr();
reg();
break;
default: printf("
NO MATCH FOUND");
printf("
Press Enter to re-Enter the choice");
if(getch()==13)
clrscr();
goto XY;
}
return 0;
}
void reg()
{
FILE *fp;
char c,checker[30]; int z=0;
fp=fopen("Web_reg.txt","ab+");
printf("
WELCOME TO REGISTER ZONE");
printf("
^^^^^^^^^^^^^^^^^^^^^^^^");
for(i=0;i<100;i++)
{
printf("
ENTER USERNAME: ");
scanf("%s",checker);
while(!feof(fp))
{
fread(&w[i],sizeof(w[i]),1,fp);
if(strcmp(checker,w[i].name)==0)
{
printf("
USERNAME ALREDY EXISTS");
clrscr();
reg();
}
else
{
strcpy(w[i].name,checker);
break;
}
}
printf("
DESIRED PASSWORD: ");
while((c=getch())!=13)
{
w[i].pass[z++]=c;
printf("%c",'*');
}
fwrite(&w[i],sizeof(w[i]),1,fp);
fclose(fp);
printf("
Press enter if you agree with Username and Password");
if((c=getch())==13)
{
clrscr();
printf("
You are successfully registered");
printf("
Try login your account??
");
printf("> PRESS 1 FOR YES
> PRESS 2 FOR NO
");
scanf("%d",&n);
switch(n)
{
case 1: clrscr();
login();
break;
case 2: clrscr();
printf("
THANK YOU FOR REGISTERING");
break;
}
}
break;
}
getch();
}
void login()
{
FILE *fp;
char c,name[30],pass[30]; int z=0;
int checku,checkp;
fp=fopen("Web_reg.txt","rb");
printf("
WELCOME TO LOG IN ZONE");
printf("
^^^^^^^^^^^^^^^^^^^^^^");
for(i=0;i<1000;i++)
{
printf("
ENTER USERNAME: ");
scanf("%s",name);
printf("
ENTER PASSWORD: ");
while((c=getch())!=13)
{
pass[z++]=c;
printf("%c",'*');
}
pass[z]='';
while(!feof(fp))
{
fread(&w[i],sizeof(w[i]),1,fp);
checku=strcmp(name,w[i].name);
checkp=strcmp(pass,w[i].pass);
if(checku==0&&checkp==0)
{
clrscr();
printf("
YOU HAVE LOGGED IN SUCCESSFULLY!!");
printf("
WELCOME, HAVE A NICE DAY");
break;
}
else if(checku==0&&checkp!=0)
{
printf("
WRONG PASSWORD!! Not %s??",name);
printf("
(Press 'Y' to re-login)");
if(getch()=='y'||getch()=='Y')
login();
}
else if(checku!=0)
{
printf("
You are not a Registered User
Press enter to register yourself");
if(getch()==13)
clrscr();
reg();
}
}
break;
}
getch();
}
Update:
Following is the updated User login and registration program in C:
#include
#include
#define MAX_USERS 100
#define MAX_USERNAME_LENGTH 20
#define MAX_PASSWORD_LENGTH 20
struct User {
char username[MAX_USERNAME_LENGTH];
char password[MAX_PASSWORD_LENGTH];
};
struct User users[MAX_USERS];
int numUsers = 0;
void registerUser() {
if (numUsers == MAX_USERS) {
printf("Maximum number of users reached.\n");
return;
}
struct User newUser;
printf("Enter username: ");
scanf("%s", newUser.username);
printf("Enter password: ");
scanf("%s", newUser.password);
users[numUsers] = newUser;
numUsers++;
printf("Registration successful.\n");
}
int loginUser() {
char username[MAX_USERNAME_LENGTH];
char password[MAX_PASSWORD_LENGTH];
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
for (int i = 0; i < numUsers; i++) {
if (strcmp(username, users[i].username) == 0 && strcmp(password, users[i].password) == 0) {
printf("Login successful.\n");
return 1;
}
}
printf("Invalid username or password.\n");
return 0;
}
int main() {
int choice;
do {
printf("1. Register\n");
printf("2. Login\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
registerUser();
break;
case 2:
if (loginUser()) {
// Perform actions for logged-in user
}
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 3);
return 0;
}
Explaination:
In this program, we use a struct
called User
to represent a user with a username and password. We maintain an array users
to store registered users, with a maximum capacity defined by MAX_USERS
. The variable numUsers
keeps track of the current number of registered users.
The registerUser()
function prompts the user to enter a username and password, stores them in a new User
struct, and adds it to the users
array.
The loginUser()
function prompts the user to enter a username and password and checks if they match any of the registered users. If a match is found, it prints a success message; otherwise, it prints an error message.
The main()
function presents a menu with options for registration, login, and exit. It repeatedly asks the user for a choice and performs the corresponding action until the user chooses to exit.
Note that this is a simplified example for learning purposes and not meant for production use. It does not include error handling, input validation, or secure password storage.