Akkhileshh CP Project
Akkhileshh CP Project
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;
};
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;
}
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");
}
if (tempFile == NULL) {
printf("Error opening temporary file.\n");
return;
}
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");
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:
Program Output:
Book added successfully!
2. View Books
User Interaction:
Program Output:
List of Books:
3. Update Book
User Interaction:
Program Output:
Book record updated!
4. Delete Book
User Interaction:
Program Output:
Book record deleted!
5. Exit
User Interaction:
Program Output:
Exiting program...
AKKHILESHH UPPU
24911A04K6
ECE-D