Thread: Help in checking password in a file

  1. #31
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Lokesh View Post
    i think you should use the
    strcmp function which returns 0(zero) if the two strings are same
    syntax:

    Code:
    x=strcmp(user,fuser);
    if(x==0)
    {
    y=strcmp(pass,fpass);
    if(y==0)
    {
    printf("authrised user");
    }
    }
    this will surely work
    i have used it before.....
    what should i declare x and y and i??

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by frodonet View Post
    The loop is reading the data from the file....but i don think it's looping cause i have entered the user id and password before the loop...

    so then the loop can check one time for the user ...

    i think i understand, if i press wrong username...it thus shows username not found...

    but when i press correct password...it still close the program
    The logic is like this:
    Read one line from file, store in line_buffer.
    Condition for loop is if the read succeeds. In other words, if the read fails, the loop breaks.
    Then you break the data into separate variables.
    You check the user and fuser strings to see if they're equal. If yes, then you check if the pass and fpass is equal. If yes, then print a message and wait for a keypress.
    Then continue the loop, since you haven't entered any break or return.
    Loop.

    Quote Originally Posted by Lokesh View Post
    i think you should use the
    strcmp function which returns 0(zero) if the two strings are same
    syntax:

    Code:
    x=strcmp(user,fuser);
    if(x==0)
    {
    y=strcmp(pass,fpass);
    if(y==0)
    {
    printf("authrised user");
    }
    }
    this will surely work
    i have used it before.....
    Quote Originally Posted by frodonet View Post
    what should i declare x and y and i??
    That's the code I've already presented to you. No need to do it again.

  3. #33
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Elysia View Post
    The logic is like this:
    Read one line from file, store in line_buffer.
    Condition for loop is if the read succeeds. In other words, if the read fails, the loop breaks.
    Then you break the data into separate variables.
    You check the user and fuser strings to see if they're equal. If yes, then you check if the pass and fpass is equal. If yes, then print a message and wait for a keypress.
    Then continue the loop, since you haven't entered any break or return.
    Loop.
    sorry to made you so mad.

    it's just that i'm kind of dumb. please forgive me, i knew i asked so many questions.

    so sorry...

  4. #34
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not mad. Just telling you where the problem lies
    You should remember that code execution always continues forward unless you tell the compiler it shouldn't. To do that, you can use break (breaks a loop), continue (jumps to the beginning of the loop again) or return (exits the function).
    There's also goto, but many consider it bad. Goto jumps to a specific place in your code. Take care when using it, though, since it can really make your code look messy.
    Last edited by Elysia; 10-30-2007 at 02:22 PM.

  5. #35
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Elysia View Post
    Not mad. Just telling you where the problem lies
    You should remember that code execution always continues forward unless you tell the compiler it shouldn't. To do that, you can use break (breaks a loop), continue (jumps to the beginning of the loop again) or return (exits the function).
    There's also goto, but many consider it bad. Goto jumps to a specific place in your code. Take care when using it, though, since it can really make your code look messy.
    i have put breaks...it still the same thing....

  6. #36
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Code:
    #include<stdio.h>
    
    main( )
     {
         FILE *fpassword;
         char c, line_buffer[100], user[20], pass[5];
         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 (strcmp(user, fuser) == 0) /* OK, found the username the user inputted */
                      {
    	                 if (strcmp(pass, fpass) == 0) /* Now, check if passwords match, if they do not, user entered a faulty password */
                         {
                         printf("User authorized");
                         getchar();
                         }
                         else
                         {
                         printf("User not authorized");
                         getchar();
                         }
                      }
                }
          }
        fclose(fpassword);
    
        getchar();
     }
    arghhh... the program still closes after i pressed userid and password.

    i just couldn't figure it out help me

  7. #37
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    5 chars for a password?
    Since one of them will be \0, and the other one will be a \n (for a complete line), that leaves you with 3 chars.
    Type any more than that, and you're out of sync in the rest of the program.

    With fgets(), use an array of BUFSIZ characters, then validate/process that buffer to it's correct variable in your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #38
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by Salem View Post
    5 chars for a password?
    Since one of them will be \0, and the other one will be a \n (for a complete line), that leaves you with 3 chars.
    Type any more than that, and you're out of sync in the rest of the program.
    i have changed my pass[5] into pass[20]...


    Quote Originally Posted by Salem View Post
    With fgets(), use an array of BUFSIZ characters, then validate/process that buffer to it's correct variable in your program.
    how do i do this??? how to validate??

  9. #39
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Validate in this case refers to checking the name/password.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #40
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by matsp View Post
    Validate in this case refers to checking the name/password.

    --
    Mats
    checking the name and password inputted or checking name and password with the file?

    any examples?? this BUFSIZ is really new to me...

  11. #41
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    BUFSIZ is just a constant that is defined to be some value specific to the compiler/system.

  12. #42
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by MacGyver View Post
    BUFSIZ is just a constant that is defined to be some value specific to the compiler/system.
    so how do i use it to check name and password??

  13. #43
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, in this case, validation could also be considered "validating that the input is correct".

    For example, if you use fgets() to get a short string, and someone types a longer string, the next fgets() will get the remaining data from the input buffer.

    The input buffer is BUFSIZ long, so we can't get more data than a BUFSIZ at a time [most likely].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #44
    Registered User
    Join Date
    Sep 2007
    Posts
    45
    Quote Originally Posted by matsp View Post
    Well, in this case, validation could also be considered "validating that the input is correct".

    For example, if you use fgets() to get a short string, and someone types a longer string, the next fgets() will get the remaining data from the input buffer.

    The input buffer is BUFSIZ long, so we can't get more data than a BUFSIZ at a time [most likely].

    --
    Mats
    i would appreciate an example...a simple one. there's no way a beginner can understand a c code just by sentences.

  15. #45
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the "example" would depend on what you need to do in "validating the input". For example, if you are asking for a username that is supposed to be 20 chars long, then you may do something like:
    Code:
        char temp[BUFSIZ];
        char username[21] = "!!InvalidUser!!";  // Make a default name that can't be used
        printf("Username: ");
        if (fgets(temp, BUFSIZ, stdin) != NULL)
        {
           // Remove newline if it's there. 
           if (temp[strlen(temp)-1]  == '\n') 
              temp[strlen(temp)-1] = 0; 
           if (strlen(temp) > sizeof(username)-1)  {  
                /// username is too long  - tell user, or just ignore it [leaving the "default" invalid name in there].
           } else {
              strcpy(username, temp);
           }
        }
        ...
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

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