Hi Everyone!!

I'm facing a few problems in storing the structutres or records in a file.
I'm trying to store the usernames and passwords(Taken as example), of a person in a single record as structure variable w[99]. and **i=0 to i<=100
***
When the program Executes for the first time, the username and password of a person are stored in rec[0] record... But when I close everything and restart the program, again it starts from w[0], which results in merging of all records..

and since all data is in a single record, if any of the username and passwords match the existing record, the program logs in. Whichmust not happen..

**Needed sugesstions.. **

code:

#include<conio.h>
#include<stdio.h>
#include<string.h>
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();
 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();

    case 2: clrscr();
         reg();
    default: printf("NO MATCH FOUND");
         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<1000;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&&checkp!=0)
          {
            printf("\n\n\n\t\t\tYou are not a Registered User\n \t\t\tPress enter to register yourself");
            if(getch()==13)
             reg();
          }

        }
        break;
      }
      getch();

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

What is the use of this loop.? First clear you logic what you want to do and then start your coding.
and 1 tips fom my side that opening file mode instead of "wb+"..use "w".
You always start you loop from i=0; why not you decalare a global variable which represent the number of record in your file and start from the next to that.

Edited 4 Years Ago by fox_3: update

You need to put break statement on lines 27 and 30 so that if you select Login the program won't make you register also.

and 1 tips fom my side that opening file mode instead of "wb+"..use "w".

No. "wb+" is correct in this program because the structures are being written in binary mode using fwrite().at line 67.

Edited 4 Years Ago by Ancient Dragon

@fox3 wb+ is the best mode for writing structs., as ancient dragon said.

@Ancient Dragon
that is ok!! i'l write break there, but each time when i run the program, the i value will be reinitialized to zero, so again the records will be merged isn't it?
what if i take i as static??? will that solve the problem?

Edited 4 Years Ago by Vish0203

This article has been dead for over six months. Start a new discussion instead.