Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 KB | None | 0 0
  1. #include    <stdio.h>
  2. #include    <unistd.h>
  3. #include    <string.h>
  4. #include    <stdlib.h>
  5.  
  6. char * createlogin(char filename[50]);
  7. char * checkusername(char filename[50]);
  8. char * checkpin(char filename[50]);
  9.  
  10. int main(void){
  11.  
  12.     int     done = 0;
  13.     char    loginorcreate[7], filename[50];
  14.    
  15.     while(!done){
  16.     sleep(1);
  17.     printf("'login' or 'create' new user?:   ");
  18.     scanf("%s", &loginorcreate);   
  19.    
  20.     if (strcmp(loginorcreate, "login") == 0){
  21.         checkusername(filename);
  22.     }
  23.     else if (strcmp(loginorcreate, "create") == 0){
  24.         createlogin(filename);
  25.     }
  26.     else{
  27.         printf("exiting...\n");
  28.         return(0);
  29.         done = 1;
  30.     }
  31.     }
  32.    
  33. }
  34.  
  35. char * createlogin(char filename[50]){
  36.     FILE *userfile;
  37.     char username[20],  pin[20];
  38.    
  39.     printf("Create a username:   ");
  40.     scanf("%s", &username);
  41.     printf("Create a password:   ");
  42.     scanf("%s", &pin);
  43.    
  44.     sprintf(filename, "C:/programming/users/%s.txt", username);
  45.    
  46.     userfile = fopen(filename, "w+");
  47.    
  48.     fprintf(userfile, "%s\n%s\n", username, pin);
  49.     fclose(userfile);
  50.    
  51.     for(int i = 0; i < 3; i++){
  52.         printf(".");
  53.         sleep(1);
  54.     }
  55.    
  56.     printf("\nUser created\n");
  57.     sleep(1);
  58.    
  59.    
  60.     return filename;
  61. }
  62.  
  63. char * checkusername(char filename[50]){
  64.     FILE    *userfile;
  65.     char    readusername[20], loginusername[20];
  66.     int     found_username = 0;
  67.    
  68.     printf("Username:   ");
  69.     scanf("%s", &loginusername);
  70.    
  71.     sprintf(filename, "C:/programming/users/%s.txt", loginusername);
  72.     userfile = fopen(filename, "r");
  73.    
  74.     while(fgets(readusername, 20, userfile) != 0){
  75.         if((strstr(readusername, loginusername)) != 0){
  76.             printf("Username found\n");
  77.             checkpin(filename);
  78.             found_username++;
  79.         }
  80.     }
  81.     if (found_username == 0){
  82.         printf("Wrong username\n");
  83.         fclose(userfile);
  84.         return(0);
  85.     }
  86.    
  87.     }
  88.    
  89. char * checkpin(char filename[50]){
  90.     FILE    *userfile;
  91.     char    pin[20], pininfile[20];
  92.     int     found_pin = 0;
  93.    
  94.     printf("Enter your password:   ");
  95.     scanf("%s", &pin);
  96.    
  97.     userfile = fopen(filename, "r");
  98.    
  99.     while(fgets(pininfile, 20, userfile) != 0){
  100.         if((strstr(pininfile, pin)) != 0){
  101.             printf("Login succesful\n");
  102.             found_pin++;
  103.             fclose(userfile);
  104.         }
  105.     }
  106.     if (found_pin == 0){
  107.         printf("Wrong password\n");
  108.         fclose(userfile);
  109.         return(0);
  110.     }
  111.    
  112.    
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement