1. Home >
  2. Apps >
  3. Groups >

User Login and Registration using Files in C

Question asked by Vishal Sharma in #Coffee Room on Jun 17, 2012
Vishal Sharma
Vishal Sharma 路 Jun 17, 2012
Rank B3 - LEADER
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<conio.h>
#include<stdio.h>
#include<string.h>
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("\n\n\n\n\n\t\t\t\tWELCOME TO MY WEBSITE");
printf("\n\t\t\t\t=====================");
printf("\n\n\n\n\t\t\tPress Enter to proceed...!!");
if(getch()==13)
  clrscr();
XY:
printf("\n\n\n\t\t\t1. LOGIN\t\t2. REGISTER");
printf("\n\n\n\t\t\t\tENTER YOUR CHOICE: ");
scanf("%d",&n);
switch(n)
  {
    case 1: clrscr();
        login();
        break;
    case 2: clrscr();
        reg();
        break;
    default: printf("\n\n\t\t\t\tNO MATCH FOUND");
        printf("\n\n\t\t\tPress 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("\n\n\t\t\t\tWELCOME TO REGISTER ZONE");
    printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^");
    for(i=0;i<100;i++)
    {
      printf("\n\n\t\t\t\t  ENTER USERNAME: ");
      scanf("%s",checker);
        while(!feof(fp))
        {
          fread(&w[i],sizeof(w[i]),1,fp);
          if(strcmp(checker,w[i].name)==0)
            {
            printf("\n\n\t\t\tUSERNAME ALREDY EXISTS");
            clrscr();
            reg();
            }
          else
          {
            strcpy(w[i].name,checker);
            break;
          }
        }
      printf("\n\n\t\t\t\t  DESIRED PASSWORD: ");
      while((c=getch())!=13)
        {
          w[i].pass[z++]=c;
          printf("%c",'*');
        }
      fwrite(&w[i],sizeof(w[i]),1,fp);
      fclose(fp);
      printf("\n\n\tPress enter if you agree with Username and Password");
      if((c=getch())==13)
        {
        clrscr();
        printf("\n\n\t\tYou are successfully registered");
        printf("\n\n\t\tTry login your account??\n\n\t\t  ");
        printf("> PRESS 1 FOR YES\n\n\t\t  > PRESS 2 FOR NO\n\n\t\t\t\t");
        scanf("%d",&n);
        switch(n)
          {
              case 1: clrscr();
                    login();
                    break;
              case 2: clrscr();
                    printf("\n\n\n\t\t\t\t\tTHANK 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("\n\n\t\t\t\tWELCOME TO LOG IN ZONE");
      printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^");
      for(i=0;i<1000;i++)
      {
        printf("\n\n\t\t\t\t  ENTER USERNAME: ");
        scanf("%s",name);
        printf("\n\n\t\t\t\t  ENTER PASSWORD: ");
        while((c=getch())!=13)
        {
          pass[z++]=c;
          printf("%c",'*');
        }
        pass[z]='\0';
      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("\n\n\n\t\t\tYOU HAVE LOGGED IN SUCCESSFULLY!!");
            printf("\n\n\n\t\t\t\tWELCOME, HAVE A NICE DAY");
            break;
          }
        else if(checku==0&&checkp!=0)
          {
            printf("\n\n\n\t\t\tWRONG PASSWORD!! Not %s??",name);
            printf("\n\n\t\t\t\t  (Press 'Y' to re-login)");
            if(getch()=='y'||getch()=='Y')
              login();
          }
        else if(checku!=0)
          {
            printf("\n\n\n\t\t\tYou are not a Registered User\n \t\t\tPress enter to register yourself");
            if(getch()==13)
            clrscr();
            reg();
          }
        }
        break;
      }
      getch();
    }
Posted in: #Coffee Room
Kaustubh Katdare
Kaustubh Katdare 路 Jul 12, 2012
Rank A1 - PRO
We've an unanswered thread, folks! Where are all our computing experts? 馃榾
Uday Bidkar
Uday Bidkar 路 Jul 14, 2012
Rank D3 - MASTER
I don't have a C compiler with me, but do see some potential errors in code.

0) The way you check for "user name exist" is faulty. You are not going through entire file to search the username. The first failure to match the user name breaks the loop.
1) I can see you are using recursion in case user name already exists. Close the file pointer before the call to reg() and call "return" just after you call reg(). This will ensure that the calling reg() wont continue below that point when the called reg() returns.
Recursion is extremely great tool and at the same time very dangerous if not used right 馃榾
2) You don't initialize the value of z to zero before setting the password for a new user

Try fixing these first and we'll take take it from there later.
Vishal Sharma
Vishal Sharma 路 Jul 14, 2012
Rank B3 - LEADER
Uday Bidkar
I don't have a C compiler with me, but do see some potential errors in code.

0) The way you check for "user name exist" is faulty. You are not going through entire file to search the username. The first failure to match the user name breaks the loop.
1) I can see you are using recursion in case user name already exists. Close the file pointer before the call to reg() and call "return" just after you call reg(). This will ensure that the calling reg() wont continue below that point when the called reg() returns.
Recursion is extremely great tool and at the same time very dangerous if not used right 馃榾
2) You don't initialize the value of z to zero before setting the password for a new user

Try fixing these first and we'll take take it from there later.

Thanks for your reply!! but i've fixed this error!! 馃槈
ibrar
ibrar 路 Oct 28, 2013
Rank E2 - BEGINNER
Can you please share you code. I have some requirement in my program and it will be highly appreciated if you can help here :


1.Login Screen

When use enters user name and password in the login screen should be matched against

and encrypted flat file and allowed access if match is successful for userid

Not of invalid attempts should be logged and id disabled when more than consecutive 5 attempts
ibrar
ibrar 路 Oct 28, 2013
Rank E2 - BEGINNER
Add/delete users
i.Supervisor id should allowed to update new users to encrypted password file GUI
ii.Supervisor id should be allowed to delete new users from encrypted password file entered with GUI
Vishal Sharma
Vishal Sharma 路 Oct 28, 2013
Rank B3 - LEADER
This is a very old thread!
Problem has been rectified long back!
denish aarons
denish aarons 路 Apr 12, 2014
Rank E2 - BEGINNER
Vishal0203 how did u fix the program, really need to know, please get back to me
Joshua Dani M
Joshua Dani M 路 Oct 17, 2016
Rank E1 - BEGINNER
vishal0203 could you please send the final code...it would be really helpful
Joshua Dani M
Joshua Dani M 路 Oct 17, 2016
Rank E1 - BEGINNER
Vishal0203
This is a very old thread!
Problem has been rectified long back!
could you send the rectified code...it would help in my mini project
Joshua Dani M
Joshua Dani M 路 Oct 19, 2016
Rank E1 - BEGINNER
@Vishal0203 please send the complete code!
Vishal Sharma
Vishal Sharma 路 Oct 19, 2016
Rank B3 - LEADER
Joshua Dani M
@Vishal0203 please send the complete code!
Hey, I'm afraid i do not have this code with me anymore, as you see this is a 4-5 year old thread. However, you can use struct to solve this.

Open the file in binary mode, write the struct with username and password to the file. You can then load the data of the file as initialization and store it in array of structs. Then take the input of username and password and check your array for the match.

Note : this solution is not optimal, but my requirements weren't that great.
Joshua Dani M
Joshua Dani M 路 Oct 19, 2016
Rank E1 - BEGINNER
Vishal0203
Hey, I'm afraid i do not have this code with me anymore, as you see this is a 4-5 year old thread. However, you can use struct to solve this.

Open the file in binary mode, write the struct with username and password to the file. You can then load the data of the file as initialization and store it in array of structs. Then take the input of username and password and check your array for the match.

Note : this solution is not optimal, but my requirements weren't that great.
Thank you! But i'm not that great in programming! could you modify the above program...it will be very helpful to me...and ill be grateful to you
Vishal Sharma
Vishal Sharma 路 Oct 19, 2016
Rank B3 - LEADER
Joshua Dani M
Thank you! But i'm not that great in programming! could you modify the above program...it will be very helpful to me...and ill be grateful to you
No assignment helps.
Joshua Dani M
Joshua Dani M 路 Oct 19, 2016
Rank E1 - BEGINNER
Vishal0203
No assignment helps.
Thanks anyways! Btw great code and idea!
Joshua Dani M
Joshua Dani M 路 Oct 19, 2016
Rank E1 - BEGINNER
Joshua Dani M
Thank you! But i'm not that great in programming! could you modify the above program...it will be very helpful to me...and ill be grateful to you
But isnt that what you did in the code @Vishal0203 ? Or am i mistaken?
Vishal Sharma
Vishal Sharma 路 Oct 23, 2016
Rank B3 - LEADER
Joshua Dani M
But isnt that what you did in the code @Vishal0203 ? Or am i mistaken?
Yeah I did some modifications in the same code to get it work.
Just not interested to study C code anymore 馃榾
eagha
eagha 路 Apr 7, 2018
Rank E2 - BEGINNER
pls send crt program bro
alberth ruado
alberth ruado 路 Oct 27, 2018
Rank E1 - BEGINNER

#include<conio.h>

#include<stdio.h>

#include<string.h>

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("\n\n\n\n\n\t\t\t\tWELCOME TO MY WEBSITE");

printf("\n\t\t\t\t=====================");

printf("\n\n\n\n\t\t\tPress Enter to proceed...!!");

if(getch()==13)

聽 clrscr();

XY:

printf("\n\n\n\t\t\t1. LOGIN\t\t2. REGISTER");

printf("\n\n\n\t\t\t\tENTER YOUR CHOICE: ");

scanf("%d",&n);

switch(n)

聽 {

聽 聽 case 1: clrscr();

聽 聽 聽 聽 login();

聽 聽 聽 聽 break;

聽 聽 case 2: clrscr();

聽 聽 聽 聽 reg();

聽 聽 聽 聽 break;

聽 聽 default: printf("\n\n\t\t\t\tNO MATCH FOUND");

聽 聽 聽 聽 printf("\n\n\t\t\tPress 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("\n\n\t\t\t\tWELCOME TO REGISTER ZONE");

聽 聽 printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^^^");

聽 聽 for(i=0;i<100;i++)

聽 聽 {

聽 聽 聽 printf("\n\n\t\t\t\t 聽ENTER USERNAME: ");

聽 聽 聽 scanf("%s",checker);

聽 聽 聽 聽 while(!feof(fp))

聽 聽 聽 聽 {

聽 聽 聽 聽 聽 fread(&w[i],sizeof(w[i]),1,fp);

聽 聽 聽 聽 聽 if(strcmp(checker,w[i].name)==0)

聽 聽 聽 聽 聽 聽 {

聽 聽 聽 聽 聽 聽 printf("\n\n\t\t\tUSERNAME ALREDY EXISTS");

聽 聽 聽 聽 聽 聽 clrscr();

聽 聽 聽 聽 聽 聽 reg();

聽 聽 聽 聽 聽 聽 }

聽 聽 聽 聽 聽 else

聽 聽 聽 聽 聽 {

聽 聽 聽 聽 聽 聽 strcpy(w[i].name,checker);

聽 聽 聽 聽 聽 聽 break;

聽 聽 聽 聽 聽 }

聽 聽 聽 聽 }

聽 聽 聽 printf("\n\n\t\t\t\t 聽DESIRED PASSWORD: ");

聽 聽 聽 while((c=getch())!=13)

聽 聽 聽 聽 {

聽 聽 聽 聽 聽 w[i].pass[z++]=c;

聽 聽 聽 聽 聽 printf("%c",'*');

聽 聽 聽 聽 }

聽 聽 聽 fwrite(&w[i],sizeof(w[i]),1,fp);

聽 聽 聽 fclose(fp);

聽 聽 聽 printf("\n\n\tPress enter if you agree with Username and Password");

聽 聽 聽 if((c=getch())==13)

聽 聽 聽 聽 {

聽 聽 聽 聽 clrscr();

聽 聽 聽 聽 printf("\n\n\t\tYou are successfully registered");

聽 聽 聽 聽 printf("\n\n\t\tTry login your account??\n\n\t\t 聽");

聽 聽 聽 聽 printf("> PRESS 1 FOR YES\n\n\t\t 聽> PRESS 2 FOR NO\n\n\t\t\t\t");

聽 聽 聽 聽 scanf("%d",&n);

聽 聽 聽 聽 switch(n)

聽 聽 聽 聽 聽 {

聽 聽 聽 聽 聽 聽 聽 case 1: clrscr();

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 login();

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 break;

聽 聽 聽 聽 聽 聽 聽 case 2: clrscr();

聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 printf("\n\n\n\t\t\t\t\tTHANK 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("\n\n\t\t\t\tWELCOME TO LOG IN ZONE");

聽 聽 聽 printf("\n\t\t\t\t^^^^^^^^^^^^^^^^^^^^^^");

聽 聽 聽 for(i=0;i<1000;i++)

聽 聽 聽 {

聽 聽 聽 聽 printf("\n\n\t\t\t\t 聽ENTER USERNAME: ");

聽 聽 聽 聽 scanf("%s",name);

聽 聽 聽 聽 printf("\n\n\t\t\t\t 聽ENTER PASSWORD: ");

聽 聽 聽 聽 while((c=getch())!=13)

聽 聽 聽 聽 {

聽 聽 聽 聽 聽 pass[z++]=c;

聽 聽 聽 聽 聽 printf("%c",'*');

聽 聽 聽 聽 }

聽 聽 聽 聽 pass[z]='\0';

聽 聽 聽 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("\n\n\n\t\t\tYOU HAVE LOGGED IN SUCCESSFULLY!!");

聽 聽 聽 聽 聽 聽 printf("\n\n\n\t\t\t\tWELCOME, HAVE A NICE DAY");

聽 聽 聽 聽 聽 聽 break;

聽 聽 聽 聽 聽 }

聽 聽 聽 聽 else if(checku==0&&checkp!=0)

聽 聽 聽 聽 聽 {

聽 聽 聽 聽 聽 聽 printf("\n\n\n\t\t\tWRONG PASSWORD!! Not %s??",name);

聽 聽 聽 聽 聽 聽 printf("\n\n\t\t\t\t 聽(Press 'Y' to re-login)");

聽 聽 聽 聽 聽 聽 if(getch()=='y'||getch()=='Y')

聽 聽 聽 聽 聽 聽 聽 login();

聽 聽 聽 聽 聽 }

聽 聽 聽 聽 else if(checku!=0)

聽 聽 聽 聽 聽 {

聽 聽 聽 聽 聽 聽 printf("\n\n\n\t\t\tYou are not a Registered User\n \t\t\tPress enter to register yourself");

聽 聽 聽 聽 聽 聽 if(getch()==13)

聽 聽 聽 聽 聽 聽 clrscr();

聽 聽 聽 聽 聽 聽 reg();

聽 聽 聽 聽 聽 }

聽 聽 聽 聽 }

聽 聽 聽 聽 break;

聽 聽 聽 }

聽 聽 聽 getch();

聽 聽 }

Kaustubh Katdare
Kaustubh Katdare 路 Oct 27, 2018
Rank A1 - PRO

Posting on behalf of @tulluru

Registration process:

the system should check the admin is a existing user,if he is not then the system should check the admin is a student or a faculty.If he is a student then the system should take all the required details for the registration process and make a username and password for the new user to login the college website in future.the same is required for the faculty too.

Deepika Tankasala
Deepika Tankasala 路 Dec 29, 2018
Rank E1 - BEGINNER

I have tried to execute the code but there is a problem.Once a person has registered and tries to login the system still says "you aren't a registered user" could you help in this regard.Please reply at the earliest!

Rakibul Pranto
Rakibul Pranto 路 Feb 25, 2019
Rank E1 - BEGINNER

I have tried to execute the code but there is a problem.twice a person has registered and tries to login the system it says "you aren't a registered user" could you help in this regard.Please reply at the earliest! Tomorrow i have to submit my project to my teacher

Hassan shaikh
Hassan shaikh 路 Nov 29, 2019
Rank E1 - BEGINNER

Rakibul Pranto,How did you solve this problem? I'd really appreciate if you reply ASAP.

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up