khalilov 0 Report post Posted January 12, 2009 (edited) Iam making a project for my university in C language that requires a log in function. 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 clientvoid 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.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: 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. Edited January 12, 2009 by khalilov (see edit history) Share this post Link to post Share on other sites
yordan 10 Report post Posted January 16, 2009 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. Share this post Link to post Share on other sites
khalilov 0 Report post Posted January 16, 2009 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 =) Share this post Link to post Share on other sites
yordan 10 Report post Posted January 16, 2009 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 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. Share this post Link to post Share on other sites
khalilov 0 Report post Posted January 16, 2009 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 :/ Share this post Link to post Share on other sites
yordan 10 Report post Posted January 16, 2009 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. Share this post Link to post Share on other sites