Library Management System in C (Complete Example with File Handling)


This project demonstrates a Library Management System in C, allowing you to add, display, search, issue, and return books. It uses structures, arrays, and file handling to store and retrieve book records efficiently.

1. Features

  1. Add new books
  2. Display all books
  3. Search a book by ID or title
  4. Issue a book
  5. Return a book
  6. Save and load records using files

2. Structure Definition


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Book {
int id;
char title[50];
char author[50];
int isIssued; // 0 = available, 1 = issued
};

3. File-Based Operations


#define FILENAME "books.dat"

// Add book
void addBook() {
struct Book b;
FILE *fp = fopen(FILENAME, "ab");
if(!fp) { printf("Error opening file.\n"); return; }

printf("Enter Book ID: "); scanf("%d", &b.id);
printf("Enter Title: "); scanf(" %[^\n]", b.title);
printf("Enter Author: "); scanf(" %[^\n]", b.author);
b.isIssued = 0; // available

fwrite(&b, sizeof(b), 1, fp);
fclose(fp);
printf("Book added successfully!\n");
}

// Display all books
void displayBooks() {
struct Book b;
FILE *fp = fopen(FILENAME, "rb");
if(!fp) { printf("No records found.\n"); return; }

printf("\nID\tTitle\tAuthor\tStatus\n");
while(fread(&b, sizeof(b), 1, fp)) {
printf("%d\t%s\t%s\t%s\n", b.id, b.title, b.author, b.isIssued ? "Issued" : "Available");
}
fclose(fp);
}

// Search book by ID
void searchBook() {
int searchId;
struct Book b;
printf("Enter Book ID to search: "); scanf("%d", &searchId);

FILE *fp = fopen(FILENAME, "rb");
if(!fp) { printf("No records found.\n"); return; }

int found = 0;
while(fread(&b, sizeof(b), 1, fp)) {
if(b.id == searchId) {
printf("ID: %d\nTitle: %s\nAuthor: %s\nStatus: %s\n", b.id, b.title, b.author, b.isIssued ? "Issued" : "Available");
found = 1;
break;
}
}
if(!found) printf("Book not found.\n");
fclose(fp);
}

// Issue a book
void issueBook() {
int bookId;
struct Book b;
FILE *fp = fopen(FILENAME, "rb+");
if(!fp) { printf("No records found.\n"); return; }

printf("Enter Book ID to issue: "); scanf("%d", &bookId);
int found = 0;

while(fread(&b, sizeof(b), 1, fp)) {
if(b.id == bookId) {
if(b.isIssued) {
printf("Book already issued!\n");
} else {
b.isIssued = 1;
fseek(fp, -sizeof(b), SEEK_CUR);
fwrite(&b, sizeof(b), 1, fp);
printf("Book issued successfully!\n");
}
found = 1;
break;
}
}
if(!found) printf("Book not found.\n");
fclose(fp);
}

// Return a book
void returnBook() {
int bookId;
struct Book b;
FILE *fp = fopen(FILENAME, "rb+");
if(!fp) { printf("No records found.\n"); return; }

printf("Enter Book ID to return: "); scanf("%d", &bookId);
int found = 0;

while(fread(&b, sizeof(b), 1, fp)) {
if(b.id == bookId) {
if(!b.isIssued) {
printf("Book is not issued!\n");
} else {
b.isIssued = 0;
fseek(fp, -sizeof(b), SEEK_CUR);
fwrite(&b, sizeof(b), 1, fp);
printf("Book returned successfully!\n");
}
found = 1;
break;
}
}
if(!found) printf("Book not found.\n");
fclose(fp);
}

4. Main Menu


int main() {
int choice;
while(1) {
printf("\n--- Library Management System ---\n");
printf("1. Add Book\n2. Display Books\n3. Search Book\n4. Issue Book\n5. Return Book\n6. Exit\n");
printf("Enter your choice: "); scanf("%d", &choice);

switch(choice) {
case 1: addBook(); break;
case 2: displayBooks(); break;
case 3: searchBook(); break;
case 4: issueBook(); break;
case 5: returnBook(); break;
case 6: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

5. Key Points to Remember

  1. Uses struct to store book information
  2. File handling ensures permanent storage (fwrite, fread, rb+)
  3. Menu-driven program allows easy navigation
  4. Can be extended to delete books, update details, and track issued dates