Advertisement
Guest User

User.

a guest
May 31st, 2018
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <cs50.h>
  4. #include <ctype.h>
  5.  
  6. int main (void) {
  7. char jop=get_char("Find user <f> Add user <a>\n");
  8. jop=tolower(jop);
  9. FILE *user;
  10.  
  11. /********************************Find user***********************/
  12. if(jop == 'f'){
  13. char *u_name=get_string("Username: ");
  14. char *u_inf=malloc(strlen(u_name)+5);
  15. strcpy(u_inf, u_name);
  16. sprintf(u_inf,"%s.txt",u_name);
  17. //read file
  18. user=fopen(u_inf,"r");
  19. //Check if User is null
  20. if(user == NULL){
  21.     fprintf(stderr,"Couldn't find user\n");
  22.     fclose(user);
  23.     return 2;
  24. }
  25. //Get data from file
  26. fread(u_inf,strlen(u_name)+19,1,user);
  27. printf("Password: %s\n",u_inf);
  28. fclose(user);
  29. return 0;
  30. /********************************Add user***********************/
  31. }
  32. if(jop == 'a'){
  33. char *u_name=get_string("Username: ");
  34. char *u_inf=malloc(strlen(u_name)+5);
  35. strcpy(u_inf, u_name);
  36. sprintf(u_inf,"%s.txt",u_name);
  37. //Write and read file
  38. user=fopen(u_inf,"w+");
  39. //Check if User was null
  40. if(user == NULL){
  41.     fprintf(stderr,"Couldn't add user\n");
  42.     fclose(user);
  43.     return 3;
  44. }
  45. //Get password from user
  46. string u_password=get_string("Password: ");
  47. //Check if password was null
  48. if(u_password == NULL){
  49.     fprintf(stderr,"Couldn't get password\n");
  50.     fclose(user);
  51.     return 4;
  52.     }
  53. //Check if password wasn't more than 13
  54.     if(strlen(u_password)>13){
  55.     fprintf(stderr,"Error password max lenth is 13\n");
  56.     fclose(user);
  57.     return 5;
  58.     }
  59.      sprintf(u_inf,"%s",u_password);
  60. //write file
  61.     fwrite(u_inf,strlen(u_inf)+1,1,user);
  62.     fseek(user,strlen(u_inf)+1,SEEK_SET);
  63.     fprintf(stderr,"file created successful\n");
  64.     fclose(user);
  65.     return 0;
  66. }
  67. if(jop!='a'|| jop!='f'){
  68.     fprintf(stderr,"Error\n");
  69.     return 1;
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement