Thread: Help in checking password in a file

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    45

    Unhappy Help in checking password in a file

    Hi,

    I need some serious help here.

    How do i check a person userid and password and compare it with a file.

    The file passwd.txt is as follows :

    tan!susan 0321 john1
    lee!jenny 0322 mary2
    wong!chris 0333 chris3

    I've managed to read the file using C.

    Code:
    #include<stdio.h>
    
    main( )
     {
         FILE *fpassword;
         char c;
         int pass;
         fpassword = fopen("C:\PASSWD.TXT", "r");
         if (fpassword == NULL) printf("File doesn't exist\n");
         else
         {
             do {
                    c = fgetc(fpassword);
                    putchar(c);
                } while (c != EOF);
    
         }
        fclose(fpassword);
    
        getchar();
     }
    But now how do i make check it's user id and password?

    Meaning if the person enters :

    Userid : john1
    Password : 0321

    It shows that the User is Authorized...?

    How???

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm not sure what is the username and what is the password in that file, but the general idea is to let the user input username/password, then the program will proceed to read each username/password combination from the file and compare those to the input. If no match is found, then the program fails.

    Input can be done with cout to display something on the screen and cin to get data from the user. Although one might cosider using another function such as cin.get since cin by itself is not safe.
    Store those in char arrays like
    Code:
    char username[100];
    , then read from the file using some read techniques, which might be done in different amount of ways. One might be to read a line, then look for spaces and copy all that data into separate buffers for username, password and whatever more there is. Then you can compare the inputted username and password with strstr, which returns 1 if both strings you compare are equal.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Read the users userid and password into two variables (character arrays). Search through the file line-by-line (not character-by-character) and find a match with the userid and then check to see if the password field matches. Reading through the file can be a call to fgets to get individual lines combined with sscanf to parse out the individual fields in the line returned by the fgets call all in a while loop. If you reach the end of the file without finding a userid match or you do find a userid match but the password doesn't match then you print out the invalid user message.

    Code:
    fpassword = fopen("C:\PASSWD.TXT", "r");
    You need to escape that slash:
    Code:
    fpassword = fopen("C:\\PASSWD.TXT", "r");
    Quote Originally Posted by Elysia
    Input can be done with cout to display something on the screen and cin to get data from the user. Although one might cosider using another function such as cin.get since cin by itself is not safe.
    This is the C board, not the C++ board.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Elysia View Post
    I'm not sure what is the username and what is the password in that file, but the general idea is to let the user input username/password, then the program will proceed to read each username/password combination from the file and compare those to the input. If no match is found, then the program fails.

    Input can be done with cout to display something on the screen and cin to get data from the user. Although one might cosider using another function such as cin.get since cin by itself is not safe.
    Store those in char arrays like
    Code:
    char username[100];
    , then read from the file using some read techniques, which might be done in different amount of ways. One might be to read a line, then look for spaces and copy all that data into separate buffers for username, password and whatever more there is. Then you can compare the inputted username and password with strstr, which returns 1 if both strings you compare are equal.
    yeah, i need to compare the inputted username and password.

    But i need to use C programming and not C++...

    So how do i copy the data i read into separate buffers??? that's the thing i've really want to know???

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hk_mp5kpdw View Post
    This is the C board, not the C++ board.
    cin and cout exists in standard C, as well, though there might be other functions to do such things.

    Quote Originally Posted by frodonet View Post
    yeah, i need to compare the inputted username and password.

    But i need to use C programming and not C++...

    So how do i copy the data i read into separate buffers??? that's the thing i've really want to know???
    strcpy perhaps. Copies a string from one buffer to another. Otherwise you can use memcpy to copy memory from one buffer to another.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    is the read function i use fgetc is not proper issit?

    cause its reading by character?

    so if i want to read by line i must use fgets?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can do that too, but it's slower than reading a whole line at a time.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by hk_mp5kpdw View Post
    Read the users userid and password into two variables (character arrays).
    How do i do this part??? sorry, but i'm seriously dumb with c.

    Quote Originally Posted by hk_mp5kpdw View Post
    Search through the file line-by-line (not character-by-character) and find a match with the userid and then check to see if the password field matches. Reading through the file can be a call to fgets to get individual lines combined with sscanf to parse out the individual fields in the line returned by the fgets call all in a while loop.
    .
    Is there any example i can refer for this part?

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Elysia View Post
    You can do that too, but it's slower than reading a whole line at a time.
    i did try fgets...but i keep getting an error saying that "there are too few arguments".

    if i want to use fgets, i must declare 2 variables for userid and password right?

    and then, when i read using fgets....how should i continue with the sscanf...

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your read loop might look something like:
    Code:
    /* Get userid and pass from the user, open the file */
    char line_buffer[100];
    while( fgets(line_buffer,sizeof(line_buffer),fpassword) != NULL )  /* read from file while still data available */
    {
        char ftemp[20], fpass[20], fuser[20];
        if( sscanf(line_buffer,"&#37;.20s %.20s %.20s",ftemp,fpass,fuser) != 3 )  /* Make sure we parsed 3 fields from the line */
        {
            /* Invalid line in file, skip to next one */
        }
        else /* Otherwise, we got 3 fields */
        {
            /* Compare fuser with whatever userid the user entered and if a match compare the passwords */
            /* If everything matches, call break to break out of loop early */
        }
    }
    Size values for the buffers above are arbitrary.

    Quote Originally Posted by frodonet
    How do i do this part??? sorry, but i'm seriously dumb with c.
    Code:
    char user[20];
    printf("Enter userid: ");
    fgets(user,sizeof(user),stdin);
    Do something similar for the password.
    Last edited by hk_mp5kpdw; 10-30-2007 at 12:28 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    cin and cout exists in standard C, as well, though there might be other functions to do such things.
    lol....

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by hk_mp5kpdw View Post

    Code:
    char user[20];
    printf("Enter userid: ");
    fgets(user,sizeof(user),stdin);
    Do something similar for the password.
    hey thanks for your guidance...

    just that i want to understand some things...

    should i read the file first or should i prompt the user for userid and password first?

  13. #13
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by frodonet View Post
    hey thanks for your guidance...

    just that i want to understand some things...

    should i read the file first or should i prompt the user for userid and password first?
    It's really completely and absolutely up to you.

    Personally, I'd read it after prompting. That way I can compare the password to the read password as it reads instead of storing the whole list in memory.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by MacGyver View Post
    lol....
    I don't know much about all those CLI-like functions I only do Windows programming in C++.

  15. #15
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by IceDane View Post
    It's really completely and absolutely up to you.

    Personally, I'd read it after prompting. That way I can compare the password to the read password as it reads instead of storing the whole list in memory.
    hi, this is my new code...i just managed to redo again.

    Code:
    #include<stdio.h>
    
    main( )
     {
         FILE *fpassword;
         char c, line_buffer[100], user[20];
         int pass;
         fpassword = fopen("C:\\PASSWD.TXT", "r");
         if (fpassword == NULL) printf("File doesn't exist\n");
    
         printf("Enter userid: ");
         fgets(user,sizeof(user),stdin);
    
         printf("Enter password: ");
         fgets(pass,sizeof(pass),stdin);
    
         while( fgets(line_buffer,sizeof(line_buffer),fpassword) != NULL )  /* read from file while still data available */
         {
                char ftemp[20], fpass[20], fuser[20];
                if( sscanf(line_buffer,"%.20s %.20s %.20s",ftemp,fpass,fuser) != 3 )  /* Make sure we parsed 3 fields from the line */
                {
                     /* Invalid line in file, skip to next one   */
                }
                else /* Otherwise, we got 3 fields */
                {
                      if (user == fuser && pass = fpassword)
                      printf("User Authorized");
                      else
                      printf("User Not Authorized");
                      exit(0);
                }
    }
        fclose(fpassword);
    
        getchar();
     }
    there's this error saying that there's invalid lvalue in my assignment.

    i think the part of my assignment of user==fuser is wrong? is it wrong?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM