Thread: NEED HELD plz (using disk files)

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28

    Smile NEED HELD plz (using disk files)

    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.

  2. #2
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    ok guys. ile be back in like an hour

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    or less

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It should be scanf(" %c") -- notice the space before the %c. As it stands now, change always gets the input "enter-key", which doesn't equal 'y'.

    Also, gets is bad (and deprecated, meaning it's going to go away One Of These Days).

  5. #5
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    Quote Originally Posted by tabstop View Post
    It should be scanf(" %c") -- notice the space before the %c. As it stands now, change always gets the input "enter-key", which doesn't equal 'y'.

    Also, gets is bad (and deprecated, meaning it's going to go away One Of These Days).
    Let me try that. I don't understand how that could make a difference though.
    Last edited by jamaican1231; 04-11-2010 at 07:53 PM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    Quote Originally Posted by jamaican1231 View Post
    Thanks let me try that. I don't understand how that could make a difference though.
    thanks 4 trying but it didn't fix the problem.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    Neeeeeeeeeed help

  8. #8
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    wtf. Changing gets() to scanf actually worked. Thanks dude

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess the question is this: why are you mixing scanf and gets? They don't work well together, as we are seeing (scanf leaves the enter-key behind, which gets picks up on instead of the input you want). Just change all your gets to scanf's and everyone will be happy.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Location
    kingston jamaica
    Posts
    28
    why the hell would they make the scanf function like that

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because the "f" in scanf stands for "formatted" -- so if your input follows a format it's great. If you just have people banging on a keyboard....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. files
    By Raven Arkadon in forum C++ Programming
    Replies: 2
    Last Post: 02-26-2005, 02:18 PM