Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. /***********************************************************************
  2.  * ActionOnWeightWrite.c
  3.  * Allows nurses to input a username and password
  4.  * writes the clients details to a file
  5.  * Encrypts
  6.  * Harry Bentham
  7.  * 28/11/17
  8.  * ********************************************************************/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13.  
  14. struct date
  15. {
  16.     int day;
  17.     int month;
  18.     int year;
  19. };
  20.  
  21. struct login
  22. {
  23.     char username[256];
  24.     char password[9];
  25. };
  26.  
  27. struct details
  28. {
  29.     char fullName[256];
  30.     struct date DOB;
  31.     float height;
  32.     float waist;
  33.     float weight;
  34.     char comment[256];
  35. };
  36.  
  37. int login(struct login nurse);
  38. void enterDetails(void);
  39. void emptyBuffer(void);
  40.  
  41. int main(void)
  42. {
  43.     struct login nurse;
  44.    
  45.     if(login(nurse) ==0)
  46.     {
  47.         printf("Invalid Username or Password\n");
  48.     } else
  49.     {
  50.         printf("Valid Username and Password\n");
  51.     }
  52.        
  53.     enterDetails();
  54.  
  55.     return 0;
  56. }
  57.  
  58. int login(struct login nurse)
  59. {
  60.     FILE *fptr;
  61.     struct login readNurse[6];
  62.     int i =0;
  63.     int j=0;
  64.     int k=0;
  65.     int passOrUser = 0;
  66.     char ch = '\0';
  67.    
  68.     printf("\nUsername: ");
  69.     scanf("%[^\n]", nurse.username);
  70.     emptyBuffer();
  71.     printf("\nPassword: ");
  72.     scanf("%[^\n]", nurse.password);
  73.    
  74.    
  75.     if((fptr = fopen("loginDetails.txt", "r"))== NULL)
  76.     {
  77.         printf("\ncan't open file loginDetails.txt\n");
  78.         exit(1);
  79.     }
  80.    
  81.     puts("file read");
  82.    
  83.     do
  84.     {
  85.         ch = fgetc(fptr);
  86.        
  87.         if(ch == EOF)
  88.         {
  89.             printf("\n");
  90.             goto TEST;
  91.         }else if(ch == ':')
  92.         {
  93.             printf("%c", ch);
  94.             passOrUser = 1;
  95.             k=0;
  96.         } else if(ch == '\n')
  97.         {
  98.             printf("%c", ch);
  99.             passOrUser = 0;
  100.             k=0;
  101.             i++;
  102.         } else if(passOrUser == 0)
  103.         {
  104.             printf("%c", ch);
  105.             readNurse[i].username[k] = ch;
  106.             k++;
  107.         } else if(passOrUser == 1)
  108.         {
  109.             printf("%c", ch);
  110.             readNurse[i].password[k] = ch;
  111.             k++;
  112.         } else
  113.         {
  114.             exit(1);
  115.         }
  116.     }while(1);
  117.    
  118.     TEST:
  119.    
  120.     printf("%s : %s\n", readNurse[0].username, readNurse[0].password);
  121.     printf("%s : %s\n", readNurse[1].username, readNurse[1].password);
  122.     printf("%s : %s\n", readNurse[2].username, readNurse[2].password);
  123.     printf("%s : %s\n", readNurse[3].username, readNurse[3].password);
  124.     printf("%s : %s\n", readNurse[4].username, readNurse[4].password);
  125.  
  126.     j=i;
  127.    
  128.    
  129.    
  130.     for(i=0;j+1>i;i++)
  131.     {
  132.         if(strcmp(nurse.username, readNurse[i].username)==0)
  133.         {
  134.             if(strcmp(nurse.password, readNurse[i].password)==0)
  135.             {
  136.                 fclose(fptr);
  137.                 return i++;
  138.             }
  139.         }
  140.     }
  141.    
  142.     fclose(fptr);
  143.    
  144.     return 0;
  145.        
  146. }
  147.  
  148. void enterDetails(void)
  149. {
  150.     ;
  151. }
  152.  
  153. void emptyBuffer(void)
  154. {
  155.     while(getchar() != '\n');
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement