Untitled
unknown
plain_text
9 months ago
4.2 kB
38
Indexable
//Project library management system :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_BOOKS 100
#define MAX_TITLE_LENGTH 50
typedef struct {
int id;
char title[MAX_TITLE_LENGTH];
char author[MAX_TITLE_LENGTH];
int isIssued;
char dueDate[11]; // Format: YYYY-MM-DD
} Book;
Book library[MAX_BOOKS];
int bookCount = 0;
void addBook() {
if (bookCount >= MAX_BOOKS) {
printf("Library is full! Cannot add more books.\n");
return;
}
printf("Enter Book ID: ");
scanf("%d", &library[bookCount].id);
getchar(); // Consume the newline character
printf("Enter Book Title: ");
fgets(library[bookCount].title, MAX_TITLE_LENGTH, stdin);
library[bookCount].title[strcspn(library[bookCount].title, "\n")] = 0; // Remove newline
printf("Enter Author Name: ");
fgets(library[bookCount].author, MAX_TITLE_LENGTH, stdin);
library[bookCount].author[strcspn(library[bookCount].author, "\n")] = 0; // Remove newline
library[bookCount].isIssued = 0; // Book is not issued initially
strcpy(library[bookCount].dueDate, ""); // No due date
bookCount++;
printf("Book added successfully!\n");
}
void issueBook() {
int id, found = 0;
printf("Enter Book ID to Issue: ");
scanf("%d", &id);
for (int i = 0; i < bookCount; i++) {
if (library[i].id == id) {
found = 1;
if (library[i].isIssued) {
printf("Book is already issued.\n");
} else {
library[i].isIssued = 1;
printf("Enter Due Date (YYYY-MM-DD): ");
scanf("%s", library[i].dueDate);
printf("Book issued successfully!\n");
}
break;
}
}
if (!found) {
printf("Book with ID %d not found.\n", id);
}
}
void viewBooks() {
if (bookCount == 0) {
printf("No books in the library.\n");
return;
}
printf("\nLibrary Books:\n");
printf("ID\tTitle\t\tAuthor\t\tStatus\n");
printf("---------------------------------------------\n");
for (int i = 0; i < bookCount; i++) {
printf("%d\t%s\t\t%s\t\t%s\n", library[i].id, library[i].title, library[i].author,
library[i].isIssued ? "Issued" : "Available");
}
}
void saveToFile() {
FILE *file = fopen("library.txt", "w");
if (!file) {
printf("Error saving library to file.\n");
return;
}
for (int i = 0; i < bookCount; i++) {
fprintf(file, "%d,%s,%s,%d,%s\n", library[i].id, library[i].title, library[i].author,
library[i].isIssued, library[i].dueDate);
}
fclose(file);
printf("Library saved to file successfully.\n");
}
void loadFromFile() {
FILE *file = fopen("library.txt", "r");
if (!file) {
printf("No saved library file found.\n");
return;
}
bookCount = 0;
while (fscanf(file, "%d,%49[^,],%49[^,],%d,%10s\n", &library[bookCount].id,
library[bookCount].title, library[bookCount].author,
&library[bookCount].isIssued, library[bookCount].dueDate) != EOF) {
bookCount++;
}
fclose(file);
printf("Library loaded from file successfully.\n");
}
int main() {
int choice;
loadFromFile();
do {
printf("\nLibrary Management System\n");
printf("1. Add Book\n");
printf("2. Issue Book\n");
printf("3. View Books\n");
printf("4. Save and Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
issueBook();
break;
case 3:
viewBooks();
break;
case 4:
saveToFile();
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 4);
return 0;
}
Editor is loading...
Leave a Comment