In this section, you will apply the knowledge and skills you have acquired throughout the course to implement a comprehensive project. This project will help you consolidate your understanding of C programming and demonstrate your ability to solve real-world problems using the language.
Project Overview
Project Description
You will develop a simple library management system. The system will allow users to:
- Add new books to the library.
- View the list of available books.
- Borrow and return books.
- Search for books by title or author.
Project Requirements
- Data Structures: Use structures to represent books and users.
- File Handling: Store the library data in a file to persist information between program executions.
- Dynamic Memory Allocation: Use dynamic memory allocation for managing the list of books and users.
- Functions: Implement various functionalities using functions to ensure modularity and reusability.
- Error Handling: Include error handling to manage invalid inputs and file operations.
Step-by-Step Implementation
Step 1: Define Data Structures
Define the structures for books and users.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TITLE_LENGTH 100
#define MAX_AUTHOR_LENGTH 100
#define MAX_USERS 50
typedef struct {
int id;
char title[MAX_TITLE_LENGTH];
char author[MAX_AUTHOR_LENGTH];
int is_borrowed;
} Book;
typedef struct {
int id;
char name[50];
int borrowed_book_id;
} User;Step 2: Initialize the Library
Create functions to initialize the library and load data from files.
#define MAX_BOOKS 100
Book books[MAX_BOOKS];
int book_count = 0;
void load_books() {
FILE *file = fopen("books.dat", "rb");
if (file != NULL) {
fread(&book_count, sizeof(int), 1, file);
fread(books, sizeof(Book), book_count, file);
fclose(file);
}
}
void save_books() {
FILE *file = fopen("books.dat", "wb");
fwrite(&book_count, sizeof(int), 1, file);
fwrite(books, sizeof(Book), book_count, file);
fclose(file);
}Step 3: Add New Books
Implement a function to add new books to the library.
void add_book() {
if (book_count >= MAX_BOOKS) {
printf("Library is full!\n");
return;
}
Book new_book;
new_book.id = book_count + 1;
printf("Enter book title: ");
fgets(new_book.title, MAX_TITLE_LENGTH, stdin);
new_book.title[strcspn(new_book.title, "\n")] = '\0'; // Remove newline character
printf("Enter book author: ");
fgets(new_book.author, MAX_AUTHOR_LENGTH, stdin);
new_book.author[strcspn(new_book.author, "\n")] = '\0'; // Remove newline character
new_book.is_borrowed = 0;
books[book_count++] = new_book;
save_books();
printf("Book added successfully!\n");
}Step 4: View Available Books
Implement a function to display the list of available books.
void view_books() {
printf("ID\tTitle\t\tAuthor\t\tStatus\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < book_count; i++) {
printf("%d\t%s\t\t%s\t\t%s\n", books[i].id, books[i].title, books[i].author, books[i].is_borrowed ? "Borrowed" : "Available");
}
}Step 5: Borrow and Return Books
Implement functions to borrow and return books.
void borrow_book() {
int book_id;
printf("Enter book ID to borrow: ");
scanf("%d", &book_id);
getchar(); // Consume newline character
for (int i = 0; i < book_count; i++) {
if (books[i].id == book_id) {
if (books[i].is_borrowed) {
printf("Book is already borrowed!\n");
} else {
books[i].is_borrowed = 1;
save_books();
printf("Book borrowed successfully!\n");
}
return;
}
}
printf("Book not found!\n");
}
void return_book() {
int book_id;
printf("Enter book ID to return: ");
scanf("%d", &book_id);
getchar(); // Consume newline character
for (int i = 0; i < book_count; i++) {
if (books[i].id == book_id) {
if (!books[i].is_borrowed) {
printf("Book is not borrowed!\n");
} else {
books[i].is_borrowed = 0;
save_books();
printf("Book returned successfully!\n");
}
return;
}
}
printf("Book not found!\n");
}Step 6: Search for Books
Implement a function to search for books by title or author.
void search_books() {
char query[MAX_TITLE_LENGTH];
printf("Enter title or author to search: ");
fgets(query, MAX_TITLE_LENGTH, stdin);
query[strcspn(query, "\n")] = '\0'; // Remove newline character
printf("ID\tTitle\t\tAuthor\t\tStatus\n");
printf("-------------------------------------------------\n");
for (int i = 0; i < book_count; i++) {
if (strstr(books[i].title, query) != NULL || strstr(books[i].author, query) != NULL) {
printf("%d\t%s\t\t%s\t\t%s\n", books[i].id, books[i].title, books[i].author, books[i].is_borrowed ? "Borrowed" : "Available");
}
}
}Step 7: Main Menu
Create a main menu to navigate through the different functionalities.
void main_menu() {
int choice;
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. View Books\n");
printf("3. Borrow Book\n");
printf("4. Return Book\n");
printf("5. Search Books\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // Consume newline character
switch (choice) {
case 1:
add_book();
break;
case 2:
view_books();
break;
case 3:
borrow_book();
break;
case 4:
return_book();
break;
case 5:
search_books();
break;
case 6:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 6);
}
int main() {
load_books();
main_menu();
return 0;
}Conclusion
By completing this project, you have demonstrated your ability to:
- Design and implement data structures.
- Use file handling to persist data.
- Apply dynamic memory allocation.
- Create modular and reusable functions.
- Handle errors effectively.
This project serves as a practical application of the concepts covered in the course and prepares you for more complex programming challenges. Congratulations on completing the project!
C Programming Course
Module 1: Introduction to C
- Introduction to Programming
- Setting Up the Development Environment
- Hello World Program
- Basic Syntax and Structure
Module 2: Data Types and Variables
Module 3: Control Flow
Module 4: Functions
- Introduction to Functions
- Function Arguments and Return Values
- Scope and Lifetime of Variables
- Recursive Functions
Module 5: Arrays and Strings
Module 6: Pointers
Module 7: Structures and Unions
Module 8: Dynamic Memory Allocation
Module 9: File Handling
- Introduction to File Handling
- Reading and Writing Files
- File Positioning
- Error Handling in File Operations
Module 10: Advanced Topics
Module 11: Best Practices and Optimization
- Code Readability and Documentation
- Debugging Techniques
- Performance Optimization
- Security Considerations
