Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #pragma warning (disable: 4996)
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "Functions2.h"
  6. #include "Functions.c"
  7.  
  8. char content[100];
  9. int answer = 0, length = 0, found = 0, users = 0, choice3 = 0, k = 1;
  10. long size = 0;
  11. char test[10];
  12.  
  13. int main() {
  14. /*User Struct which is used in order to create a new profile with a name, password, key and potential new
  15. files (Created after the login)*/
  16. typedef struct User {
  17. char name[16];
  18. char password[16];
  19. char key[8];
  20. char files[8][16];
  21. };
  22. struct User* User2 = NULL;
  23. struct User* currentUser = NULL;
  24.  
  25. //Loading
  26. file = fopen("Profile", "rb");
  27. if (file != NULL) {
  28. fseek(file, 0, SEEK_END);
  29. size = ftell(file);
  30. length = size / sizeof(struct User);
  31. User2 = (struct User*)malloc(size);
  32. rewind(file);
  33. fread(User2, sizeof(struct User), length, file);
  34. users = length;
  35. fclose(file);
  36.  
  37. }
  38. else {
  39. printf("Error loading data!\n");
  40. }
  41.  
  42. printf("Hello! Welcome to Final Project\n\n");
  43. while (1) {
  44. if (choice == 0) {
  45. printf("Press (1) to login\n");
  46. printf("Press (2) to create a new account\n");
  47. printf("Your choice: ");
  48. scanf("%d", &answer);
  49. getchar();
  50.  
  51. printf("\n");
  52. if (answer == 1) {
  53. char userAttempt[16];
  54. char userPassword[16];
  55. printf("Insert Username: "); scanf("%s", userAttempt);
  56. getchar();
  57. printf("\nInsert Password: "); scanf("%s", userPassword);
  58. getchar();
  59. for (int z = 0; z < 2; z++) {
  60. if (strcmp(userAttempt, User2[z].name) == 0 && strcmp(userPassword, User2[z].password) == 0) {
  61. printf("\nLogin success!");
  62. currentUser = &User2[z];
  63. choice = 1;
  64. }
  65. }
  66. }
  67. else if (answer == 2) {
  68. file = fopen("Profile", "wb");
  69. //If not found
  70.  
  71. //Read/Save.
  72. char name[16];
  73. char password[16];
  74. printf("Insert Username: "); scanf("%s", name);
  75. getchar();
  76. printf("Insert Password: "); scanf("%s", password);
  77. getchar();
  78. printf("\n");
  79.  
  80. //Create a Profile
  81. if (file == NULL) {
  82. User2 = (struct User*)malloc(sizeof(struct User));
  83. }
  84. else {
  85. User2 = (struct User*)realloc(User2, sizeof(struct User)*(users + 1));
  86. }
  87. strcpy(User2[users].name, name);
  88. strcpy(User2[users].password, password);
  89. users++;
  90.  
  91. //Save.
  92. fwrite(User2, sizeof(struct User), users, file);
  93.  
  94. //Close
  95. choice = 1;
  96. fclose(file);
  97. }
  98. else if (answer != 1, 2) {
  99. printf("\nPlease use the the number 1 and 2 to proceed.\n");
  100. getchar();
  101. }
  102. }
  103. else if (currentUser != NULL || choice == 1) {
  104. printf("\n\n What do you wish to do?");
  105. printf("\n 1) Insert Text");
  106. printf("\n 2) Save data on an existing file");
  107. printf("\n 3) Read a file");
  108. printf("\n 4) Change password");
  109. printf("\n 5) Log out");
  110. printf("\n 6) Close Program");
  111. printf("\n\nYour Choice: ");
  112. int choice2;
  113. scanf("%d", &choice2);
  114. getchar();
  115. switch (choice2) {
  116.  
  117. case 1:
  118. printFile();
  119. break;
  120. case 2:
  121. printf("What file do you wish to save data on?\n");
  122. scanf("%s", &openFile);
  123. getchar();
  124. file = fopen(openFile, "r+");
  125.  
  126. if (file == NULL) {
  127. printf("Could not open file.");
  128. getchar();
  129. return 0;
  130. } else {
  131. fscanf(file, "%s", content);
  132. }
  133. fclose(file);
  134. break;
  135. case 3:
  136. printf("\nWhat file do you wish to read?\n");
  137. printf("File: ");
  138. scanf("%s", &openFile);
  139. getchar();
  140.  
  141. file = fopen(openFile, "r");
  142.  
  143. if (file != NULL) {
  144. fseek(file, 0, SEEK_END);
  145. int lengthSeek = ftell(file);
  146. rewind(file);
  147. char *c = (char*)malloc(lengthSeek);
  148. fread(c, lengthSeek, 1, file);
  149. fclose(file);
  150. for (int u = 0; u < lengthSeek; u++) {
  151. printf("%c", c[u]);
  152. }
  153. free(c);
  154. }
  155. else if (file == NULL) {
  156. printf("File doesn't exist.. Try creating it first!");
  157. choice = 1;
  158. }
  159. break;
  160. case 4:
  161. printf("Insert new password here: ");
  162. scanf("%s", currentUser->password);
  163. getchar();
  164. break;
  165. case 5:
  166. choice = 0;
  167. break;
  168. case 6:
  169. return 0;
  170. break;
  171. default:
  172. printf("Please use the numbers 1 to 7!");
  173. choice = 1;
  174. break;
  175. getchar();
  176. }
  177. }
  178. }
  179. getchar();
  180. return (0);
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement