Admin Login

+2 Eric V K · November 23, 2014
int main ()
{

char username [15];
char pwd [20];
char userfile [15];
char pwdfile [20];
FILE *admin;

admin = fopen ("admin.txt","r");
printf("Please enter your Username\n");
scanf("%s",username);

printf("Please enter your Password\n");
scanf("%s",pwd);

if(admin != NULL){
while( !feof(admin)){

fgets(userfile, 20, admin);
fgets(pwdfile, 20, admin);

(strcmp(userfile,username) == 0 && strcmp (pwdfile,pwd) == 0);
}

printf("Welcome \n%s",username);
}

else{
printf("Please enter the correct Username/Password\n");
}

}

Hi guys, basically i'm doing my assignment which has admin login part, but the problem is how do i compare the user and password input and the one with the admin.txt file. It can compile and no error but it's not comparing the value that i have within the txt file. can you help me figure it out and tell me which part is wrong, thanks a lot ^^
and let say inside the admin.txt, the admin and password is stored.

user1;abc123
user2;12345
user3;54321

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · December 5, 2014
hi there, i have found your code a bit confusing due to code styling differences however, i have moved things around and implemented my own code with reference from yours:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {

   char username[15];
   char pwd[20];
   char userfile[15];
   char pwdfile[20];
   FILE *admin;
   int scanErr = 0;

    // open file
    admin = fopen ("admin.txt","r");
    // check if successful
    if (admin == NULL) {
        printf ("Error reading file\n");
        exit (EXIT_FAILURE);
    }
    
    // reading username input
    printf("Please enter your Username:\n");
    scanf("%s",username);
    // check for valid input
    if (strlen(username) > 15) {
        printf ("Username input too long!\n");
        exit (EXIT_FAILURE);
    }

    // reading password input
    printf("Please enter your Password:\n");
    scanf("%s",pwd);
    // check for valid input
    if (strlen(pwd) > 20) {
        printf ("Password input too long!\n");
        exit (EXIT_FAILURE);
    }
    
    // scanning username and password
    // assuming there is corrrect data in txt file
    scanErr = fscanf (admin, "%s", userfile);
    // check for errors in scanning or valid input
    if (scanErr != 1 || strlen(userfile) > 15) {
        printf ("Username scan error\n");
        exit (EXIT_FAILURE);
    }
    scanErr = fscanf (admin, "%s", pwdfile);
    // check for errors in scanning or valid input
    if (scanErr != 1 || strlen(pwdfile) > 20) {
        printf ("Password scan error\n");
        exit (EXIT_FAILURE);
    }
    
    // compare usernames and passwords
    if ((strcmp(username, userfile) == 0) && (strcmp(pwd, pwdfile) == 0)) {
        printf ("Log in successful!\nWelcome, %s!\n", username);
    } else {
        printf ("Invalid username/password.\n");
        exit (EXIT_FAILURE);
    }

   // remember to close your file!
   fclose (admin);

   return EXIT_SUCCESS;

}

i have added some potential buffer overflows by checking the validity of input lengths, you don't need t worry about that, just a security measure because, well, it is supposed to be a login program.

slight modifications:
1. i have not implemented the feof check
2. changed fgets() to fscanf() to read in lines of string separated by either a space, newline or tab resulting in
3. the change of formatting in the admin.txt file.

at the moment, the program only allows for one stored account.  if you wish to include more, you will have to add in a search function for specific usernames.  here is what an example admin.txt file should look like:
user1 passw0rd123

though if it were my own design, i would probably use command line inputs for the username and password and also have an encrypted admin.txt file which would be decrypted during runtime for reading.

hope you enjoy! :P
  • 1

C

107,049 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator