Thread: Login: Read login from a file and compare to the input

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    15

    Login: Read login from a file and compare to the input

    Hi everyone,

    I'm building a login function. I have two files - one with the user login and other with the password (needs to be this way).

    Code:
    int loginn(){
    	
    	FILE* fileusers;
    	FILE* filepassword;
    	char auxusers[MAX];
    	char auxpassword[MAX];
    	char login[MAX];
    	char password[MAX];
    	char option;
    	fileusers = fopen("../../config/users", "r");
    	filepassword = fopen("../../config/pass", "r");
    	int access = 0;
    
    	printf("Login:\n");
    	fgets(login,sizeof(login),stdin);
    	
    	printf("Password:\n");
    	fgets(password,sizeof(password),stdin);
    	
    	if( login[strlen(login)-1] == '\n')
    		login[strlen(login)-1] = 0;
    	
    	if( password[strlen(password)-1] == '\n')
    		password[strlen(password)-1] = 0;
    	
    	if(fileusers && filepassword){
    		while(((auxusers[MAX] = fgetc(fileusers)) != EOF) || (auxpassword[MAX] = fgetc(filepassword)) != EOF){
    			if((strcmp(auxusers, login) == 0) && (strcmp(auxpassword, password) == 0)){
    				printf("-- Login OK --\n");
    				access = 2;
    			}else{
    				printf("-- Login is wrong --\n");
    				printf("-- To register, press < R > or press < L > to try again. --\n");
    				
    				
    				scanf("%c", &option);
    				switch(option)
    				{
    					case 'R':	printf("-- You need to be a super user to create a new user.-- \n");
    								loginSuperUser();
    								if(access == 1)
    									createUser();
    								break;
    						
    					case 'L':	printf("Login2:\n");
    								fgets(login, sizeof(login), stdin);
    						
    								printf("Password2:\n");
    								fgets(password, sizeof(password), stdin);
    								break;
    					default: "ups\n";
    				}
    			}
    		}
    	}else{
    		perror("Error\n");
    		exit(0);
    	}
    }
    The compilation doesn't show any warning or error, however, even when I insert the right user and password, the program goes directly to the "login is wrong" part.

    Do you guys have any idea of what could be wrong?


    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    use fgets() instead of fgetc()

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    I tried, the compiler now shows the following:

    myshell.c:43: warning: passing argument 1 of ‘fgets’ from incompatible pointer type
    myshell.c:43: error: too few arguments to function ‘fgets’
    myshell.c:43: warning: passing argument 1 of ‘fgets’ from incompatible pointer type
    myshell.c:43: error: too few arguments to function ‘fgets’

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So show us your use of fgets. Sounds like you're doing it wrong.
    Code:
    char buf[ BUFSIZ ];
    
    fgets( buf, BUFSIZ, file );

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    quzah, I have fgets like you have on my first post on this thread.

    I think itCbitC is referring to the fgetc I have here:
    Code:
    while(((auxusers[MAX] = fgetc(fileusers)) != EOF) || (auxpassword[MAX] = fgetc(filepassword)) != EOF){
    Replacing it by fgets throws me that errors.

    My other fgets, as you can see above, are like you have:

    Code:
    char login[MAX];
    
    printf("Login:\n");
    fgets(login,sizeof(login),stdin);
    With the file instead of stdin, it's weird.. but it goes directly to the Wrong User part.. printfs are showed but it doesn't allow me to insert them :S I can only write them with stdin!
    Last edited by xyphen; 12-07-2009 at 06:56 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No... I said show where you replaced fgetc with fgets. You didn't show that. You are using it wrong, otherwise it wouldn't be telling you you have:
    myshell.c:43: error: too few arguments to function ‘fgets’
    You can't just change c to s and expect the function to magically work.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    You are right. Sorry for the missunderstanding.

    How can I use fgets on this line than?

    Code:
    while(((auxusers[MAX] = fgetc(fileusers)) != EOF) || (auxpassword[MAX] = fgetc(filepassword)) != EOF){

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already showed you how to use fgets. Redo the logic of your program.
    Code:
    for attempts = 0, attempts < maxattempts, attempts++
        prompt for username
        prompt for password
        if check( username, password ) == YAY
            doyay()
    doboo()

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Dec 2009
    Posts
    15
    Thank you. I got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  4. Replies: 1
    Last Post: 12-01-2002, 01:24 PM