Advertisement
Guest User

Untitled

a guest
Nov 16th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. #include    <stdio.h>
  2. #include    <unistd.h>
  3. #include    <string.h>
  4. #include    <stdlib.h>
  5. #include    <Windows.h>
  6.  
  7. char * createlogin(char filename[50]);
  8. char * checkusername(char filename[50]);
  9. char * checkpin(char filename[50]);
  10.  
  11. int main(void){
  12.  
  13.     int     done = 0;
  14.     char    loginorcreate[7], filename[50];
  15.    
  16.     while(!done){
  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.    
  42.     sprintf(filename, "C:/programming/users/%s.txt", username);
  43.    
  44.     if (access (filename, F_OK) != -1){
  45.         printf("Username already exist\n");
  46.         return(0);
  47.     }
  48.     else{
  49.        
  50.     printf("Create a password:   ");
  51.     scanf("%s", &pin);
  52.    
  53.     userfile = fopen(filename, "w+");
  54.    
  55.     fprintf(userfile, "%s\n%s\n", username, pin);
  56.     fclose(userfile);
  57.    
  58.     for(int i = 0; i < 3; i++){
  59.         printf(".");
  60.         Sleep(250);
  61.     }
  62.    
  63.     printf("\nUser created\n");
  64.     Sleep(500);
  65.     }
  66.    
  67.     return filename;
  68. }
  69.  
  70. char * checkusername(char filename[50]){
  71.     FILE    *userfile;
  72.     char    readusername[20], loginusername[20];
  73.     int     found_username = 0;
  74.    
  75.     printf("Username:   ");
  76.     scanf("%s", &loginusername);
  77.    
  78.     sprintf(filename, "C:/programming/users/%s.txt", loginusername);
  79.     userfile = fopen(filename, "r");
  80.    
  81.     while(fgets(readusername, 20, userfile) != 0){
  82.         if((strstr(readusername, loginusername)) != 0){
  83.             printf("Username found\n");
  84.             checkpin(filename);
  85.             found_username++;
  86.         }
  87.     }
  88.     if (found_username == 0){
  89.         printf("Wrong username\n");
  90.         fclose(userfile);
  91.         return(0);
  92.     }
  93.    
  94.     }
  95.    
  96. char * checkpin(char filename[50]){
  97.     FILE    *userfile;
  98.     char    pin[20], pininfile[20];
  99.     int     found_pin = 0;
  100.    
  101.     printf("Enter your password:   ");
  102.     scanf("%s", &pin);
  103.    
  104.     userfile = fopen(filename, "r");
  105.    
  106.     while(fgets(pininfile, 20, userfile) != 0){
  107.         if((strstr(pininfile, pin)) != 0){
  108.             printf("Login succesful\n");
  109.             found_pin++;
  110.             fclose(userfile);
  111.         }
  112.     }
  113.     if (found_pin == 0){
  114.         printf("Wrong password\n");
  115.         fclose(userfile);
  116.         return(0);
  117.     }
  118.    
  119.    
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement