Nov 24, 2009

Login In C Language

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Login In C Language

khalilov
Iam making a project for my university in C language that requires a log in function.
CODE
typedef struct {  int id;
[tab][/tab][tab][/tab][tab][/tab]char password[5],name[30];
[tab][/tab][tab][/tab][tab][/tab]}clients;

Thats the structure for a client

CODE
void add_client()
{FILE *file;
clients client;
file=fopen("clients.dat","r");

int i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{i++;
fread(&client,sizeof(clients),1,file);
}
client.id=i;
printf("Your id will be:%d\n",client.id);
fclose(file);
printf("Name:");

gets(client.name); flushall();
printf("Enter a 4 character password:");
i=0;
while(i<4)
{flushall();
client.password[i]=getch();
printf("*");
i++;}   client.password[4]=0;
file=fopen("clients.dat","a");
fwrite(&client,sizeof(clients),1,file);
}

Thats the function to get a client and add it to the file, i believe the most important part is the password part.
CODE
int login()
{int loginid,record,i=0; char password[4];
do{puts("Input id:");
scanf("%d",&loginid);
}while(loginid<0);  /*temp. fixing memory bug*/
puts("Input password"); flushall();
while(i<4)
{flushall();
password[i]=getch();
printf("*");
i++;}  password[4]=0;

FILE *file;
clients client;
file=fopen("clients.dat","r");
i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{printf("ID:%d Password:%s",client.id,client.password);
if((client.id==loginid)&&(!strcmp(client.password,password))){i=1; break;}

fread(&client,sizeof(clients),1,file);
}
if((client.id==loginid)&&(!strcmp(client.password,password)))i=1;
if(i) return(loginid);
return(0);
}

Thats the function which asks for a client's id and password and searches the clients' file for them. Even though the password and id i enter match it never returns 1. Can someone plz tell me where is the error and in which function. I am assuming since the conditions don't look wrong it has to do with the password getting part.

Edit:
CODE
file=fopen("clients.dat","r");

int i=0;
fread(&client,sizeof(clients),1,file);
while(!feof(file))
{i++;
fread(&client,sizeof(clients),1,file);
}
client.id=i;
printf("Your id will be:%d\n",client.id);
fclose(file);

This part reads how many records are inside the file, it used to work but for some weird reason it doesn't now, can someone tell me a possible reason? the only change that happened is that the previous client file was deleted.

 

 

 


Comment/Reply (w/o sign-up)

yordan
Seems like an error inside your own logics.
Did you try adding additional printf's, for debugging purposes ? Explicitly "printf" the entered password and the recorded password, for instance, in order to see if they are different.

Comment/Reply (w/o sign-up)

khalilov
I did and they showed that they are right, after some trial and errors it turns out that 0 isn't taken as a null character as i thought. I put password[4]='\0' and it worked. So iam guessing password[4]=0 set the the last character as zero and not null. While the passwords were being compared, it did n't stop at the zero since it is not the null character, and hence it continued and naturally it found that they aren't the same since password[5] is not defined and different everytime =)

Comment/Reply (w/o sign-up)

yordan
In a general way of designing a software, try not to use nulls, or don't step into a situation where you must manage nulls.
Today you are working with files, later (for huge amount of users) you will have to work with database tables.
And most of popular databases (like Oracle or mysql or Sybase) hate nulls, null values are absolutely against the relational concept. That's why, if you have to manage null values, your application will be very unefficient.
For instance, instead of a null, you could put Yordan (which is obviously forbidden as a password because it's a well-known word) or you could put "----" (four times the minus sign) which is something nobody would use.
You could also, instead of a comparison to null, add a test in your input section
CODE
while(i<4)
{flushall();
password[i]=getch();
printf("*");
i++;}

If the guy entered a 4-digit password, i=4 at the and of the loop. If the guy entered no password, then i=0.
So, testing the value of i greater than 0 could be a test of having entered a password.

 

 

 


Comment/Reply (w/o sign-up)

khalilov
The reason i added a null character in the end is because according to my knowledge which is limited btw , strcmp works by comparing two strings character by character until either null characters are encountered or different characters are encountered. So if i didn't use the null character in the end the strcmp would have always returned 1 (meaning the passwords are not the same), which was the reason of why the script didnt work in the first place. I could use gets(password) but that would ruin the "*" thing and the password would be viewed for everyone to see :/

Comment/Reply (w/o sign-up)

yordan
OK, now I see the need for the nulls. And of course you are right, echoing "*" instead of the input is the way we expect your program to behave.

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Login


    Looking for Login, In, C, Language

See Also,

*SIMILAR VIDEOS*
Searching Video's for Login, In, C, Language
advertisement



Login In C Language

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com