BCSL 21
BCSL 21
A structure is used to represent each book record, which includes relevant fields like ISBN,
title, author, and publisher.
c
Copy code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char isbn[13];
char title[50];
char author[50];
char publisher[50];
} Book;
The function prototypes for the operations required are defined here.
c
Copy code
void addBook(Book books[], int *count);
void displayBooks(const Book books[], int count);
int searchBookByISBN(const Book books[], int count, const char
*isbn);
void updateBook(Book books[], int count, const char *isbn);
void deleteBook(Book books[], int *count, const char *isbn);
void saveToFile(const Book books[], int count, const char
*filename);
void loadFromFile(Book books[], int *count, const char *filename);
void menu();
This function takes the book details from the user and adds it to the array.
c
Copy code
void addBook(Book books[], int *count) {
if (*count < MAX_BOOKS) {
printf("Enter ISBN: ");
scanf("%s", books[*count].isbn);
printf("Enter Title: ");
scanf(" %[^\n]", books[*count].title);
printf("Enter Author: ");
scanf(" %[^\n]", books[*count].author);
printf("Enter Publisher: ");
scanf(" %[^\n]", books[*count].publisher);
(*count)++;
printf("Book added successfully.\n");
} else {
printf("Library is full, cannot add more books.\n");
}
}
c
Copy code
void displayBooks(const Book books[], int count) {
printf("\nBook Records:\n");
for (int i = 0; i < count; i++) {
printf("ISBN: %s\nTitle: %s\nAuthor: %s\nPublisher: %s\n\n",
books[i].isbn, books[i].title, books[i].author,
books[i].publisher);
}
}
This function searches for a book by its ISBN and returns the index if found, or -1 if not.
c
Copy code
int searchBookByISBN(const Book books[], int count, const char
*isbn) {
for (int i = 0; i < count; i++) {
if (strcmp(books[i].isbn, isbn) == 0) {
return i;
}
}
return -1;
}
c
Copy code
void updateBook(Book books[], int count, const char *isbn) {
int index = searchBookByISBN(books, count, isbn);
if (index != -1) {
printf("Enter new title: ");
scanf(" %[^\n]", books[index].title);
printf("Enter new author: ");
scanf(" %[^\n]", books[index].author);
printf("Enter new publisher: ");
scanf(" %[^\n]", books[index].publisher);
printf("Book updated successfully.\n");
} else {
printf("Book not found.\n");
}
}
c
Copy code
void deleteBook(Book books[], int *count, const char *isbn) {
int index = searchBookByISBN(books, *count, isbn);
if (index != -1) {
for (int i = index; i < *count - 1; i++) {
books[i] = books[i + 1];
}
(*count)--;
printf("Book deleted successfully.\n");
} else {
printf("Book not found.\n");
}
}
6. Saving to a File
c
Copy code
void saveToFile(const Book books[], int count, const char *filename)
{
FILE *file = fopen(filename, "w");
if (file) {
for (int i = 0; i < count; i++) {
fprintf(file, "%s\n%s\n%s\n%s\n",
books[i].isbn, books[i].title, books[i].author,
books[i].publisher);
}
fclose(file);
printf("Data saved to file.\n");
} else {
printf("Error opening file.\n");
}
}
c
Copy code
void loadFromFile(Book books[], int *count, const char *filename) {
FILE *file = fopen(filename, "r");
if (file) {
*count = 0;
while (fscanf(file, "%s\n%[^\n]\n%[^\n]\n%[^\n]\n",
books[*count].isbn, books[*count].title,
books[*count].author, books[*count].publisher) != EOF) {
(*count)++;
}
fclose(file);
printf("Data loaded from file.\n");
} else {
printf("Error opening file.\n");
}
}
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n2. Display Books\n3. Search Book by
ISBN\n");
printf("4. Update Book\n5. Delete Book\n6. Save to File\n7.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addBook(books, &count); break;
case 2: displayBooks(books, count); break;
case 3:
printf("Enter ISBN to search: ");
scanf("%s", isbn);
int index = searchBookByISBN(books, count, isbn);
if (index != -1) {
printf("Book found:\nISBN: %s\nTitle:
%s\nAuthor: %s\nPublisher: %s\n",
books[index].isbn, books[index].title,
books[index].author, books[index].publisher);
} else {
printf("Book not found.\n");
}
break;
case 4:
printf("Enter ISBN to update: ");
scanf("%s", isbn);
updateBook(books, count, isbn);
break;
case 5:
printf("Enter ISBN to delete: ");
scanf("%s", isbn);
deleteBook(books, &count, isbn);
break;
case 6: saveToFile(books, count, FILENAME); break;
case 7: printf("Exiting the program.\n"); break;
default: printf("Invalid choice.\n");
}
} while (choice != 7);
}
c
Copy code
int main() {
menu();
return 0;
}
Sample Execution
1. Adding a Book
○ Enter ISBN: 1234567890123
○ Enter Title: C Programming
○ Enter Author: Dennis Ritchie
○ Enter Publisher: PHI
Displaying Books
makefile
Copy code
ISBN: 1234567890123
Title: C Programming
Author: Dennis Ritchie
Publisher: PHI
2.
3. Saving to File and Loading from File ensures data persistence between sessions.
This structure ensures effective management of book records within a library system.