Hello guys (and girls maybe) i am doing a project for school creating a database program and i am having serious problems with creating a log in function to log in users. I am trying to use a text file (the name of the text file is the same as the name of the user) to store the password. When i build the program it builds fine bu when i run the program it always ends abruptly right after the if statement asking the user whether or nor he/she wants to change the password.
ok. it would be helpful if you guys could either show me the code for a simple user log in program using files
or if you have the time
// Returns either 'd' 'o' or 's' depending on the user name entered
Code:
char login()
{
FILE *file;
char username[25];
char dentist[] = "dentist";
char entered_password[25];
char change;
puts("Enter your username");
gets(username);
if (strcmp(username,dentist) == 0)
/* || The strcmp(string x, string y) function compares 2 strings
|| It returns 0 if they are identical*/
{
puts("Enetr your passowrd (default password is password)");
gets(entered_password);
file = fopen("dentist.txt","r");
if (file == NULL)
// Prints error message if file could not be opened
{
puts("Error...................... cannot open file");
puts("the file may not exist");
}
// Opens the dentist file for reading
char password[25];
// Variable used to store the passoword in the file
fscanf(file,"%s",password);
// Reads the password and stores it in the variable password
if(strcmp(entered_password,password) == 0)
{
puts("Password correct");
fclose(file);
puts("do you want to change your passowrd (y/n)");
scanf("%c",&change);
if (change == 'y')
{
char new_password[25];
// Stores the new password
puts("Enter your new passowrd");
gets(new_password); //crashes here!!!!!!!!!!!!!!!!!!!!!
file = fopen("dentist.txt","w");
if (file == NULL)
{
puts("Error................cannot open file");
}
// Opens the dentist user file for writing
fprintf(file2,"%s",new_password);
fclose(file);
}
return 'd';
}
}
else
// Print error message
{
puts("no such username exsists");
}
}
correct this function
p.s. I don't expect you to correct the function. Ime just throwing the idea out there just in case.