-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary_management.c
108 lines (105 loc) · 2.37 KB
/
library_management.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
struct library
{
int bookno;
char bname[50];
char author[30];
int quantity;
int price;
};
struct library lib;
FILE *fp;
void add_book()
{
clrscr();
printf("\t\t\tADD BOOK DETAILS\n");
fp=fopen("libraryfile.txt","ab");
printf("Enter book number: ");
scanf("%d",&lib.bookno);
printf("Enter the book name: ");
scanf("%s",lib.bname);
printf("Enter the author: ");
scanf("%s",lib.author);
printf("Enter the quantity of this book: ");
scanf("%d",&lib.quantity);
printf("Enter the price of the book: ");
scanf("%d",&lib.price);
fwrite(&lib,sizeof(lib),1,fp);
fclose(fp);
}
void show_book()
{
clrscr();
printf("\t\t\tALL BOOK DETAILS\n");
fp=fopen("libraryfile.txt","rb");
fseek(fp,0,SEEK_SET);
if(fp==NULL)
{
printf("ERROR opening file!!!\n");
exit(0);
}
else
{
while(fread(&lib,sizeof(lib),1,fp)>0)
{
fread(&lib,sizeof(lib),1,fp);
printf("Book number: %d\n",lib.bookno);
printf("Name of the book: %s\n",lib.bname);
printf("Name of the author: %s\n",lib.author);
printf("No. of books: %d\n",lib.quantity);
printf("Price of book: %d\n\n",lib.price);
}
}
fclose(fp);
}
void total_book()
{
clrscr();
int bno=0;
printf("\t\t\tTOTAL NUMBER OF BOOKS\n");
fp=fopen("libraryfile.txt","rb");
fseek(fp,0,SEEK_SET);
if(fp==NULL)
{
printf("ERROR opening file!!!\n");
exit(0);
}
else
{
while(fread(&lib,sizeof(lib),1,fp)>0)
{
bno+=lib.quantity;
if(feof(fp))
break;
}
printf("Total number of books: %d\n\n",bno);
}
fclose(fp);
}
void main()
{
clrscr();
int choice;
while(1)
{
printf("\t\t\t\t\tLIBRARY MANAGEMENT SYSTEM\n");
printf("Enter 1 to enter new book details\n ");
printf("Enter 2 to view all book details\n");
printf("Enter 3 to view total number of books\n");
printf("Press any other key to exit\n\n");
printf("Enter your choice: ");
scanf("%d",&choice);
if(choice==1)
add_book();
else if(choice==2)
show_book();
else if(choice==3)
total_book();
else
exit(0);
}
getch();
}