[go: up one dir, main page]

0% found this document useful (0 votes)
14 views7 pages

BCSL 21

Uploaded by

tanzeelarahman31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

BCSL 21

Uploaded by

tanzeelarahman31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Here’s a detailed approach to the design and implementation of a Book-Organizing Module

for a Library Management System in C, as per the assignment specifications.

Step 1: Define the Book Structure

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>

#define MAX_BOOKS 100


#define FILENAME "library_data.txt"

typedef struct {
char isbn[13];
char title[50];
char author[50];
char publisher[50];
} Book;

Step 2: Function Prototypes

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();

Step 3: Implementing the Functions


1. Adding a New Book

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");
}
}

2. Displaying All Books

This function displays the details of all books.

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);
}
}

3. Searching for a Book by ISBN

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;
}

4. Updating a Book Record

This function allows updating a book’s details.

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");
}
}

5. Deleting a Book Record

This function deletes a book by shifting the array elements.

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

This function writes all book records 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");
}
}

7. Loading from a File

This function reads all book records from a file.

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");
}
}

Step 4: Menu Function


c
Copy code
void menu() {
Book books[MAX_BOOKS];
int count = 0;
int choice;
char isbn[13];
loadFromFile(books, &count, FILENAME);

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);
}

Step 5: Main Function

This function starts the program by calling menu().

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.

Explanation and Notes

● Modular Approach: Each function has a specific responsibility, making it easy to


maintain.
● Error Handling: Each function provides feedback to handle invalid inputs and file
operations.
● Menu Navigation: User-friendly menu for interaction.

This structure ensures effective management of book records within a library system.

You might also like