Guest User

Untitled

a guest
Sep 27th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. struct userInfo
  2. {
  3.  
  4. char* userName;
  5. char* password;
  6.  
  7. };
  8.  
  9. void signUp(const char* name, const char* password)
  10. {
  11.  
  12. userInfo* user = getNewUser(name, password);
  13.  
  14. FILE* file = fopen("/home/john/userinfo", "wb");
  15.  
  16. if (!file)
  17.  
  18. fprintf(stderr, "Error! Could not read/write file!");
  19.  
  20. else
  21.  
  22. fwrite(user, sizeof(userInfo), 1, file);
  23.  
  24. fclose(file);
  25. }
  26.  
  27. bool validateLogin(const char* name, const char* password)
  28. {
  29.  
  30. FILE* file = fopen("/home/john/userinfo", "rb");
  31.  
  32. if (!file)
  33. {
  34. fprintf(stderr, "Error! Could not read file.n");
  35. return false;
  36. }
  37. else
  38. {
  39. userInfo* user;
  40.  
  41. while (fread(user, sizeof(userInfo), 1, file))
  42. {
  43.  
  44. if ( (strcmp(name, user->userName) == 0) && (strcmp(password, user->password)== 0))
  45. {
  46.  
  47. fclose(file);
  48. return true;
  49. }
  50. }
  51. }
  52.  
  53. fclose(file);
  54. return false;
  55.  
  56. }
  57.  
  58. userInfo* getNewUser(const char* name, const char* password)
  59. {
  60.  
  61. userInfo* user = malloc(sizeof(userInfo));
  62.  
  63. user->userName = strdup(name);
  64. user->password = strdup(password);
  65.  
  66. return user;
  67. }
  68.  
  69. int main()
  70. {
  71.  
  72. int choice = 0;
  73. bool quit = false;
  74.  
  75. do
  76. {
  77. printf("Entern1. To login.n2. To sign up.nn");
  78. scanf("%1d", &choice);
  79.  
  80. if (choice >= 1 && choice <= 2)
  81. {
  82. quit = true;
  83.  
  84. if (choice == 1)
  85. {
  86.  
  87. char userName[128];
  88. char password[128];
  89.  
  90. flush();
  91.  
  92. printf("nEnter username : ");
  93. fgets(userName, 128, stdin);
  94.  
  95.  
  96. printf("nEnter password : ");
  97. fgets(password, 128, stdin);
  98.  
  99. bool isUser = validateLogin(userName, password);
  100.  
  101. if (isUser)
  102.  
  103. printf("nnLogin successfull!");
  104.  
  105. else
  106. fprintf(stderr, "nError! Invalid username or password");
  107.  
  108. }
  109. else if (choice == 2)
  110. {
  111.  
  112. char userName[128];
  113. char password[128];
  114.  
  115. flush();
  116.  
  117. printf("nEnter your username : ");
  118. fgets(userName, 128, stdin);
  119.  
  120.  
  121. printf("Enter your password : ");
  122. fgets(password, 128, stdin);
  123.  
  124. userName[strlen(userName)] = 0;
  125. password[strlen(password)] = 0;
  126.  
  127. signUp(userName, password);
  128.  
  129.  
  130. printf("nnAccount created successfully!");
  131.  
  132. }
  133.  
  134.  
  135. }
  136. else
  137. {
  138. fprintf(stderr, "nInvalid input! Try again.n");
  139. }
  140.  
  141. }while (!quit);
  142.  
  143. return 0;
  144. }
Add Comment
Please, Sign In to add comment