Advertisement
Guest User

Untitled

a guest
Feb 25th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. #include < stdio.h > #include < stdlib.h > #include < string.h >
  2.  
  3. // Copied from
  4. // https://stackoverflow.com/questions/35103745/read-a-string-as-an-input-using-scanf
  5. void flush() {
  6. int c;
  7. while ((c = getchar()) != '\n' & amp; & amp; c != EOF);
  8. }
  9.  
  10. typedef struct {
  11. char username[15];
  12. char password[15];
  13. }
  14. User;
  15.  
  16. typedef struct {
  17. int id; // must be unique
  18. char title[50];
  19. char author[50]; // if more than two, separate using ,(COMMA)
  20. char ISBN[50];
  21. char category[50];
  22. char publication[50];
  23. char description[255];
  24. int taken;
  25. }
  26. Book;
  27.  
  28. int main() {
  29. char option, admin_option, username[15], password[15], edit_option;
  30. int first_time, c, i, j, id, found;
  31. FILE * f;
  32. User user;
  33. Book book;
  34.  
  35. f = fopen("librarian.check", "r");
  36. if (f == NULL) {
  37. fclose(f);
  38. f = fopen("librarian.check", "w");
  39. fputc(1, f);
  40. fclose(f);
  41. } else {
  42. fclose(f);
  43. }
  44.  
  45. f = fopen("id.check", "r");
  46. if (f == NULL) {
  47. fclose(f);
  48. f = fopen("id.check", "w");
  49. fputc(0, f);
  50. fclose(f);
  51. } else {
  52. fclose(f);
  53. }
  54.  
  55. printf("Log in as \n");
  56. printf("1: Librarian\n");
  57. printf("2: Student\n");
  58. printf("3: Exit\n");
  59. printf("Enter your choice: ");
  60. scanf("%c", & amp; option);
  61. switch (option) {
  62. case '1':
  63. f = fopen("librarian.check", "r");
  64. if (f == NULL) {
  65. printf("Couldn't read file\n");
  66. exit(0);
  67. } else {
  68. first_time = fgetc(f);
  69. if (first_time == 1) {
  70. fclose(f);
  71. flush();
  72. printf("Provide username and password to setup\n");
  73. printf("Username (14 characters max): ");
  74. fgets(user.username, 15, stdin);
  75. printf("Password (14 characters max): ");
  76. fgets(user.password, 15, stdin);
  77.  
  78. // write this credential to file
  79. f = fopen("credential.bin", "wb");
  80. if (f == NULL) {
  81. printf("Someting went wrong!!\n");
  82. exit(0);
  83. }
  84. fwrite( & amp; user, sizeof(User), 1, f);
  85. fclose(f);
  86. printf("Exit and login again to continue\n");
  87. f = fopen("librarian.check", "w");
  88. fputc(0, f);
  89. fclose(f);
  90. } else {
  91. fclose(f);
  92. flush();
  93. printf("Provide credential to login\n");
  94. printf("Username: ");
  95. fgets(username, 15, stdin);
  96. printf("Password: ");
  97. fgets(password, 15, stdin);
  98.  
  99. // read the credential from file
  100. f = fopen("credential.bin", "rb");
  101. if (f == NULL) {
  102. printf("Someting went wrong!!\n");
  103. exit(0);
  104. }
  105. fread( & amp; user, sizeof(User), 1, f);
  106. if (strcmp(username, user.username) != 0 || strcmp(password, user.password) != 0) {
  107. printf("Username or password invalid\n");
  108. exit(0);
  109. }
  110. printf("Login Successful!!\n");
  111. printf("1: Add Book\n");
  112. printf("2: Search Book\n");
  113. printf("3: Edit Book\n");
  114. printf("4: Delete Book\n");
  115. printf("4: Moderate Student Request\n");
  116. printf("Enter your choice: ");
  117. scanf("%c", & amp; admin_option);
  118. switch (admin_option) {
  119. case '1':
  120. flush();
  121. printf("Provide the following information\n");
  122. printf("Title: ");
  123. fgets(book.title, 50, stdin);
  124. printf("Author: ");
  125. fgets(book.author, 50, stdin);
  126. printf("ISBN: ");
  127. fgets(book.ISBN, 50, stdin);
  128. printf("Category: ");
  129. fgets(book.category, 50, stdin);
  130. printf("Publication: ");
  131. fgets(book.publication, 50, stdin);
  132. printf("Description: ");
  133. fgets(book.description, 50, stdin);
  134. book.taken = 0;
  135.  
  136. // increment ID
  137. f = fopen("id.check", "r");
  138. if (f == NULL) {
  139. printf("Someting went wrong\n");
  140. exit(1);
  141. }
  142.  
  143. id = fgetc(f);
  144. fclose(f);
  145. book.id = id;
  146.  
  147. // save the record
  148. f = fopen("book.record", "a");
  149. fwrite( & amp; book, sizeof(Book), 1, f);
  150. fclose(f);
  151.  
  152. id++;
  153. f = fopen("id.check", "w");
  154. fputc(id, f);
  155. fclose(f);
  156.  
  157. printf("Book insertion Successful!!\n");
  158. break;
  159. case '2':
  160. printf("Enter the book id to search: ");
  161. scanf("%d", & amp; id);
  162.  
  163. // search in the database
  164. f = fopen("book.record", "rb");
  165. found = 0;
  166. i = 0;
  167. while (fread( & amp; book, sizeof(Book), 1, f)) {
  168. if (book.id == id) {
  169. // matched
  170. found = 1;
  171. printf("Book Found!!\n");
  172. printf("Title: %s", book.title);
  173. printf("Author: %s", book.author);
  174. printf("ISBN: %s", book.ISBN);
  175. printf("Category: %s", book.category);
  176. printf("Publication: %s", book.publication);
  177. printf("Description: %s", book.description);
  178. break;
  179. }
  180. i++;
  181. }
  182. if (found == 0) {
  183. printf("Sorry!! The book is not in the database\n");
  184. }
  185. fclose(f);
  186. break;
  187. case '3':
  188. printf("Enter the book id to edit: ");
  189. scanf("%d", & amp; id);
  190. f = fopen("book.record", "rb+");
  191. found = 0;
  192. while (fread( & amp; book, sizeof(Book), 1, f)) {
  193. if (book.id == id) {
  194. // matched
  195. found = 1;
  196. break;
  197. }
  198. }
  199. if (found == 0) {
  200. printf("Sorry!! The book is not in the database\n");
  201. } else {
  202. printf("What field do you want to edit:\n");
  203. printf("1. Title\n");
  204. printf("2. Author\n");
  205. printf("3. ISBN\n");
  206. printf("4. Category\n");
  207. printf("5. Publication\n");
  208. printf("6. Description\n");
  209. printf("Enter your choice: ");
  210. scanf("\n%c", & amp; edit_option);
  211. switch (edit_option) {
  212. case '1':
  213. flush();
  214. printf("Enter new title: ");
  215. fgets(book.title, 50, stdin);
  216. break;
  217. case '2':
  218. printf("Enter new author: ");
  219. fgets(book.author, 50, stdin);
  220. break;
  221. case '3':
  222. printf("Enter new ISBN: ");
  223. fgets(book.ISBN, 50, stdin);
  224. break;
  225. case '4':
  226. printf("Enter new Category: ");
  227. fgets(book.category, 50, stdin);
  228. break;
  229. case '5':
  230. printf("Enter new Publication: ");
  231. fgets(book.publication, 50, stdin);
  232. break;
  233. printf("Enter new Description: ");
  234. fgets(book.description, 255, stdin);
  235. break;
  236. default:
  237. printf("Enter 1 to 6\n");
  238. break;
  239. }
  240. fseek(f, i, SEEK_SET);
  241. fwrite( & amp; book, sizeof(Book), 1, f);
  242. fclose(f);
  243. printf("Book record modified in the database!!\n");
  244. }
  245. break;
  246. case '4':
  247. printf("Enter the book id to Delete: ");
  248. scanf("%d", & amp; id);
  249. f = fopen("book.record", "rb");
  250. found = 0;
  251. while (fread( & amp; book, sizeof(Book), 1, f)) {
  252. if (book.id == id) {
  253. // matched
  254. found = 1;
  255. break;
  256. }
  257. }
  258. fclose(f);
  259. if (found == 0) {
  260. printf("Sorry!! The book is not in the database\n");
  261. } else {
  262. // create a temporary file
  263. FILE * temp;
  264. temp = fopen("book.temp", "a");
  265. if (temp == NULL) {
  266. printf("Something went wrong\n");
  267. exit(1);
  268. }
  269.  
  270. f = fopen("book.record", "rb");
  271.  
  272. // copy all the items except item to delete
  273. // to temporary file
  274. while (fread( & amp; book, sizeof(Book), 1, f)) {
  275. if (book.id != id)
  276. fwrite( & amp; book, sizeof(Book), 1, temp);
  277. }
  278. fclose(f);
  279. fclose(temp);
  280.  
  281. // delete original file
  282. remove("book.record");
  283.  
  284. // rename the temporary file
  285. rename("book.temp", "book.record");
  286. printf("Book record deleted from the database!!\n");
  287. }
  288. break;
  289. default:
  290. printf("Enter only 1 - 4\n");
  291. break;
  292. }
  293. }
  294. }
  295.  
  296. break;
  297. case '2':
  298. printf("Enter the book id to search: ");
  299. scanf("%d", & amp; id);
  300.  
  301. // search in the database
  302. f = fopen("book.record", "rb");
  303. found = 0;
  304. i = 0;
  305. while (fread( & amp; book, sizeof(Book), 1, f)) {
  306. if (book.id == id) {
  307. // matched
  308. found = 1;
  309. printf("Book Found!!\n");
  310. printf("Title: %s", book.title);
  311. printf("Author: %s", book.author);
  312. printf("ISBN: %s", book.ISBN);
  313. printf("Category: %s", book.category);
  314. printf("Publication: %s", book.publication);
  315. printf("Description: %s", book.description);
  316. printf("Status: ");
  317. if (book.taken == 1) {
  318. printf("Not Available");
  319. } else {
  320. printf("Available\n");
  321. }
  322. break;
  323. }
  324. i++;
  325. }
  326. if (found == 0) {
  327. printf("Sorry!! The book is not in the database\n");
  328. }
  329. fclose(f);
  330. break;
  331. case '3':
  332. printf("Bye!!\n");
  333. exit(0);
  334. default:
  335. printf("Enter either 1 or 2 only\n");
  336. break;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement