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