Hi
I have to write a program that asks a password from user to access a file , then options for searching, adding, modifying & deleting a record of employee - a structure containing int, string*2, float.
I decided to write password in starting of the file then '\n' and then the structure data.
this is my code for password thing:
Code:
FILE *file;
if( ! ( file = fopen("employee.txt", "r+") ) )
{
printf("Error Opening in one of the modes. Program Terminating ...");
delay(1000);
exit(1)
}
char fpass[20], pass[20];
fgets(pass, 20, file);
for(ch=0; pass[ch] != '\n'; ch++)
strcat(fpass, pass[ch]);
fseek(file, strlen(fpass) + 1, SEEK_SET);
strcpy(pass, "");
while(strcmp(fpass, pass))
{
system("cls");
printf("\n\n\n\n\n\n\n\t\t\tEnter Password: ");
scanf("%s" pass);
retries += 1;
if(retries == 5)
{
printf("\n\n\t\t\t\tRetry Limit Exceeded! Program Terminating ...\n");
delay(1000);
exit(1);
}
}
I know this is not exactly what one would call a password protection, so not sarcasm please :P
But then I thought I should add an option to change password, which is the main problem right now!
I thought I can save password in file as : "mypassword \n" so that total length is 20 (max). then just put new password and while reading password initially, change:
Code:
for(ch=0; pass[ch] != '\n'; ch++)
strcat(fpass, pass[ch]);
to
Code:
for(ch=0; pass[ch] != '\n' && pass[ch] != ' '; ch++)
strcat(fpass, pass[ch]);
Is this really a stupid way ( answer ignoring that this is not actual protection ... same old story ) ?
Also, I would love to know a better and not too difficult than this method to password protect.
Thanks for bearing through this boring post !!
Cheers !!
EDIT:
There are 10 spaces before '\n' in "mypassword \n"