0% found this document useful (0 votes)
10 views6 pages

Akkhileshh CP Project

The document outlines a Library Management System project implemented in C, which allows users to add, view, update, and delete book records. It includes code for managing book details using file handling and data structures, along with user interaction examples for each operation. The system is designed to provide hands-on experience with programming concepts such as arrays and functions.
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)
10 views6 pages

Akkhileshh CP Project

The document outlines a Library Management System project implemented in C, which allows users to add, view, update, and delete book records. It includes code for managing book details using file handling and data structures, along with user interaction examples for each operation. The system is designed to provide hands-on experience with programming concepts such as arrays and functions.
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/ 6

Library Management System in C

Objective: Build a basic library management system to perform operations like adding
books, viewing book details, updating records, and deleting books. This project
provides hands-on experience with arrays, functions, file handling, and data structures.

Code:

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

struct Book {
int bookID;
char title[100];
char author[100];
int copies;
};

void addBook(FILE *fp);


void viewBooks(FILE *fp);
void updateBook(FILE *fp);
void deleteBook(FILE *fp);

int main() {
int choice;
FILE *fp;

fp = fopen("library.dat", "rb+");
if (fp == NULL) {
fp = fopen("library.dat", "wb+");
if (fp == NULL) {
printf("Error creating file!");
return 1;
}
}

do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. View Books\n");
printf("3. Update Book\n");
printf("4. Delete Book\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
addBook(fp);
break;
case 2:
viewBooks(fp);
break;
case 3:
updateBook(fp);
break;
case 4:
deleteBook(fp);
break;
case 5:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 5);

fclose(fp);
return 0;
}

void addBook(FILE *fp) {


struct Book b;
printf("Enter Book ID: ");
scanf("%d", &b.bookID);
printf("Enter Book Title: ");
scanf(" %[^\n]%*c", b.title);
printf("Enter Author Name: ");
scanf(" %[^\n]%*c", b.author);
printf("Enter Number of Copies: ");
scanf("%d", &b.copies);

fseek(fp, 0, SEEK_END); // Move file pointer to the end


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

void viewBooks(FILE *fp) {


struct Book b;
fseek(fp, 0, SEEK_SET); // Move file pointer to the start
printf("\nList of Books:\n");
while (fread(&b, sizeof(struct Book), 1, fp)) {
printf("\nBook ID: %d\n", b.bookID);
printf("Title: %s\n", b.title);
printf("Author: %s\n", b.author);
printf("Copies: %d\n", b.copies);
}
}
void updateBook(FILE *fp) {
int bookID;
struct Book b;
printf("Enter the Book ID to update: ");
scanf("%d", &bookID);

fseek(fp, 0, SEEK_SET);
while (fread(&b, sizeof(struct Book), 1, fp)) {
if (b.bookID == bookID) {
printf("Enter new number of copies for \"%s\": ", b.title);
scanf("%d", &b.copies);
fseek(fp, -sizeof(struct Book), SEEK_CUR);
fwrite(&b, sizeof(struct Book), 1, fp);
printf("Book record updated!\n");
return;
}
}
printf("Book not found!\n");
}

void deleteBook(FILE *fp) {


int bookID;
struct Book b;
FILE *tempFile;
tempFile = fopen("temp.dat", "wb+");

if (tempFile == NULL) {
printf("Error opening temporary file.\n");
return;
}

printf("Enter Book ID to delete: ");


scanf("%d", &bookID);

fseek(fp, 0, SEEK_SET);
while (fread(&b, sizeof(struct Book), 1, fp)) {
if (b.bookID != bookID) {
fwrite(&b, sizeof(struct Book), 1, tempFile);
}
}

fclose(fp);
fclose(tempFile);

remove("library.dat");
rename("temp.dat", "library.dat");

printf("Book record deleted!\n");


}
How It Works:
1. Add Book:
Allows the user to add new books with details like Book ID, title, author, and the number of copies.
Data is appended to the file.

2. View Books:
Displays all book details stored in the file.

3. Update Book:
Modifies the number of copies of an existing book based on its Book ID.

4. Delete Book:
Deletes a book by creating a temporary file, copying all other records to it, and replacing the
original file.

Features:

1. Add Contact:
Prompts the user to enter details like name, phone number, and email address. The data is saved in a
file.

2. View Contacts:
Displays all saved contacts in a readable format.

3. Search Contact:
Allows the user to search for a contact by name.

Output:

1. Add Book

User Interaction:

Library Management System


1. Add Book
2. View Books
3. Update Book
4. Delete Book
5. Exit
Enter your choice: 1
Enter Book ID: 101
Enter Book Title: C Programming Basics
Enter Author Name: Brian Kernighan
Enter Number of Copies: 5

Program Output:
Book added successfully!

2. View Books

User Interaction:

Library Management System


1. Add Book
2. View Books
3. Update Book
4. Delete Book
5. Exit
Enter your choice: 2

Program Output:
List of Books:

Book ID: 101


Title: C Programming Basics
Author: Brian Kernighan
Copies: 5

Book ID: 102


Title: Data Structures
Author: Mark Allen Weiss
Copies: 3

3. Update Book

User Interaction:

Library Management System


1. Add Book
2. View Books
3. Update Book
4. Delete Book
5. Exit
Enter your choice: 3

Enter the Book ID to update: 101


Enter new number of copies for "C Programming Basics": 10

Program Output:
Book record updated!

4. Delete Book
User Interaction:

Library Management System


1. Add Book
2. View Books
3. Update Book
4. Delete Book
5. Exit
Enter your choice: 4

Enter Book ID to delete: 102

Program Output:
Book record deleted!

5. Exit

User Interaction:

Library Management System


1. Add Book
2. View Books
3. Update Book
4. Delete Book
5. Exit
Enter your choice: 5

Program Output:
Exiting program...

AKKHILESHH UPPU
24911A04K6
ECE-D

You might also like