DESIGN DETAILS
1. Struct Definitions:
Student Struct (`struct student`):
- `int id`: Student ID.
- `char sName[50]`: Student name.
- `char sClass[50]`: Student class.
- `int sRoll`: Student roll number.
- `char bookName[50]`: Name of the book issued to the student.
Books Struct (`struct books`):
- `int id`: Book ID.
- `char bookName[50]`: Book name.
- `char authorName[50]`: Author's name.
- `int count`: Number of available copies of the book.
2. File Handling:
The program uses file handling to store information about books (`books.dat`) and issued
books (`issue.dat`).
3. Functions:
`addBook()` Function:
- Opens `books.dat` in append binary mode.
- Accepts input for book details (id, name, author, count) from the user.
- Writes the book information to the file.
`booksList()` Function:
- Displays a list of available books from the `books.dat` file.
`issueBook()` Function:
- Accepts a book ID from the user.
- Checks if the book is available.
- If available, decrements the book count and records the student details in the `issue.dat`
file.
`returnBook()` Function:
- Accepts a student roll number and book ID from the user.
- Displays books issued to the specified student.
- If the book is returned, increments the book count and updates the `books.dat` file.
- Updates the `issue.dat` file.
`issueList()` Function:
- Displays a list of books issued to students from the `issue.dat` file.
4. Menu-Driven Interface (`main()` Function):
- A menu-driven interface allows the user to choose from various options:
1. Add Book
2. Books List
3. Issue Book
4. Issued Book List
5. Return Book
6. Exit
5. User Input and Output:
- Uses `printf()` and `scanf()` for user prompts and input.
- Display messages to inform the user about the success or failure of operations.
6. Looping:
- The program runs in an infinite loop until the user chooses to exit (`case 6`).
7. Error Handling:
- Checks for file opening errors.
- Validates user inputs in some cases.
- Displays appropriate error messages.
8. Modularity:
- Functions are used to modularize the code, making it more readable and maintainable.
9. Clearing Screen:
- Uses `system("cls")` to clear the console screen.
10. Non-Standard Libraries:
- Uses `conio.h` for clearing the screen and `getch()` for pausing execution. Note that these are
non-standard and platform-dependent.
11. Platform Independence:
- Some operations, such as clearing the screen, might not be platform-independent.
12. Magic Numbers:
- Replace magic numbers with named constants for better code readability.
13. Comments:
- There are comments throughout the code to explain the purpose of different sections.
14. Input Buffer:
- Uses `fflush(stdin)` for clearing the input buffer, which is not recommended. Consider
alternative methods.
MODULE- WISE
DESCRIPTION
Data Structures:
Two structures are defined: struct student for student information and struct books for book
information.
The program uses a global file pointer fp to handle file operations.
addBook() Function:
Adds a book to the library system.
Takes input for book details such as ID, name, author, and count.
Appends the book information to the "books.dat" file.
booksList() Function:
Displays a list of available books.
Reads book information from the "books.dat" file and prints it on the console.
issueBook() Function:
Handles the process of issuing a book to a student.
Checks if the specified book is available.
If available, decreases the count of available copies, adds student information, and appends the
record to the "issue.dat" file.
returnBook() Function:
Manages the return of a book by a student.
Displays books issued to a specific student based on their roll number.
Takes input for the book to be returned and updates the count of available copies.
issueList() Function:
Displays a list of issued books with student details.
Reads information from the "issue.dat" file and prints it on the console.
main() Function:
The main function provides a menu-driven interface for users.
Options include adding a book, listing books, issuing a book, listing issued books, returning a
book, and exiting the program.
Menu Options:
Users can interact with the program by choosing options from the menu.
Each option corresponds to a specific function in the program.
Infinite Loop:
The program runs in an infinite loop, allowing users to perform multiple operations without
restarting.
File Handling:
The program uses file operations (fopen, fwrite, fread, fclose) to store and retrieve book and
student information.
PROGRAM CODE
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
struct student {
int id;
char sName[50];
char sClass[50];
int sRoll;
char bookName[50];
} s;
struct books {
int id;
char bookName[50];
char authorName[50];
int count; // added count for each book
} b;
FILE *fp;
void addBook() {
fp = fopen("books.dat", "ab");
printf("Enter book id: ");
scanf("%d", &b.id);
printf("Enter book name: ");
fflush(stdin);
fgets(b.bookName, sizeof(b.bookName), stdin);
b.bookName[strcspn(b.bookName, "\n")] = '\0'; // Remove the newline character
printf("Enter author name: ");
fflush(stdin);
fgets(b.authorName, sizeof(b.authorName), stdin);
b.authorName[strcspn(b.authorName, "\n")] = '\0'; // Remove the newline character
printf("Enter the number of copies: ");
scanf("%d", &b.count);
printf("Book Added Successfully\n");
fwrite(&b, sizeof(b), 1, fp);
fclose(fp);
}
void booksList() {
system("cls");
printf("<== Available Books ==>\n\n");
printf("%-10s %-30s %-20s %-10s\n", "Book id", "Book Name", "Author", "Count");
fp = fopen("books.dat", "rb");
while (fread(&b, sizeof(b), 1, fp) == 1) {
printf("%-10d %-30s %-20s %-10d\n", b.id, b.bookName, b.authorName, b.count);
}
fclose(fp);
}
void issueBook() {
int f = 0;
system("cls");
printf("<== Issue Books ==>\n\n");
printf("Enter Book id to issue: ");
scanf("%d", &s.id);
// Check if we have a book of the given id
fp = fopen("books.dat", "rb+");
if (fp == NULL) {
printf("Error opening books.dat\n");
return;
}
while (fread(&b, sizeof(b), 1, fp) == 1) {
if (b.id == s.id && b.count > 0) {
strcpy(s.bookName, b.bookName);
f = 1;
break;
}
}
if (f == 0) {
fclose(fp);
if (b.count == 0) {
printf("No copies available for this book\n");
} else {
printf("No book found with this id\n");
}
printf("Please try again...\n\n");
return;
}
// Decrease the count of available copies
b.count--;
fseek(fp, -sizeof(b), SEEK_CUR);
fwrite(&b, sizeof(b), 1, fp);
fclose(fp);
// Append the record to the issue.dat file
fp = fopen("issue.dat", "ab");
if (fp == NULL) {
printf("Error opening issue.dat\n");
return;
}
printf("Enter Student Name: ");
fflush(stdin);
fgets(s.sName, sizeof(s.sName), stdin);
s.sName[strcspn(s.sName, "\n")] = '\0'; // Remove the newline character
printf("Enter Student Class: ");
fflush(stdin);
fgets(s.sClass, sizeof(s.sClass), stdin);
s.sClass[strcspn(s.sClass, "\n")] = '\0'; // Remove the newline character
printf("Enter Student Roll: ");
scanf("%d", &s.sRoll);
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);
printf("Book Issued Successfully\n\n");
}
void returnBook() {
int rollNo, bookId, f = 0;
system("cls");
printf("<== Return Books ==>\n\n");
printf("Enter Student Roll No: ");
scanf("%d", &rollNo);
FILE *fr; // File pointer for reading issue.dat
FILE *fw; // File pointer for writing temp.dat
fr = fopen("issue.dat", "rb");
if (fr == NULL) {
printf("Error opening issue.dat\n");
return;
}
fw = fopen("temp.dat", "wb");
if (fw == NULL) {
printf("Error opening temp.dat\n");
fclose(fr);
return;
}
printf("Books issued to student with Roll No %d:\n", rollNo);
printf("%-10s %-30s %-10s\n", "Book id", "Book Name", "Author");
while (fread(&s, sizeof(s), 1, fr) == 1) {
if (rollNo == s.sRoll) {
printf("%-10d %-30s %-10s\n", s.id, s.bookName, s.sName);
f = 1;
} else {
fwrite(&s, sizeof(s), 1, fw);
}
}
fclose(fr);
if (f == 0) {
printf("\n\nNo books found for the specified Roll No.\n");
remove("temp.dat"); // Delete the temporary file if no record is found
return;
}
fclose(fw);
printf("\nEnter Book id to return: ");
scanf("%d", &bookId);
fr = fopen("issue.dat", "rb");
if (fr == NULL) {
printf("Error opening issue.dat\n");
return;
}
fw = fopen("temp.dat", "wb");
if (fw == NULL) {
printf("Error opening temp.dat\n");
fclose(fr);
return;
}
f = 0; // Reset the flag for reuse
while (fread(&s, sizeof(s), 1, fr) == 1) {
if (bookId == s.id && rollNo == s.sRoll) {
printf("Book returned by %s\n", s.sName);
f = 1;
// Update the count in "books.dat"
fp = fopen("books.dat", "rb+");
if (fp == NULL) {
printf("Error opening books.dat\n");
fclose(fw);
return;
}
while (fread(&b, sizeof(b), 1, fp) == 1) {
if (bookId == b.id) {
b.count++;
fseek(fp, -sizeof(b), SEEK_CUR);
fwrite(&b, sizeof(b), 1, fp);
break;
}
}
fclose(fp);
} else {
fwrite(&s, sizeof(s), 1, fw);
}
}
fclose(fr);
fclose(fw);
if (f == 0) {
printf("\n\nBook not found for the specified Book id and Roll No.\n");
remove("temp.dat"); // Delete the temporary file if no record is found
return;
}
remove("issue.dat");
rename("temp.dat", "issue.dat");
printf("Press Any Key To Continue...");
getch();
}
void issueList() {
system("cls");
printf("<== Book Issue List ==>\n\n");
printf("%-10s %-30s %-20s %-10s\n", "S.id", "Name", "Class", "Roll");
fp = fopen("issue.dat", "rb");
if (fp == NULL) {
printf("Error opening issue.dat\n");
return;
}
while (fread(&s, sizeof(s), 1, fp) == 1) {
printf("%-10d %-30s %-20s %-10d\n", s.id, s.sName, s.sClass, s.sRoll);
}
fclose(fp);
}
int main() {
int ch;
while (1) {
system("cls");
printf("<== Library Management System ==>\n");
printf("1.Add Book\n");
printf("2.Books List\n");
printf("3.Issue Book\n");
printf("4.Issued Book List\n");
printf("5.Return Book\n");
printf("6.Exit\n\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch) {
case 0:
exit(0);
case 1:
addBook();
break;
case 2:
booksList();
break;
case 3:
issueBook();
break;
case 4:
issueList();
break;
case 5:
returnBook();
break;
default:
printf("Invalid Choice...\n\n");
}
printf("Press Any Key To Continue...");
getch();
}
return 0;
}
FLOWCHART
SAMPLE OUTPUT