-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.c
213 lines (173 loc) · 4.74 KB
/
library.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// #include <caca.h>
// #include <caca_conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
struct books{
int id;
char bookName[50];
char authorName[50];
char date[12];
} bk;
struct student{
int id;
char studentName[50];
char courseName[50];
int studentRollNo;
char bookName[50];
char date[12];
} std;
FILE *fp;
void addBook();
void bookList();
void removeBook();
void issueBook();
void issueBookList();
int main() {
int choice;
do {
system("clear");
printf("<== Library Management System ==>\n");
printf("1.Add Books\n");
printf("2.Books List\n");
printf("3.Remove Book\n");
printf("4.Issue Book\n");
printf("5.Issued Book List\n");
printf("0.EXIT\n\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 0:
exit(0);
case 1:
addBook();
break;
case 2:
bookList();
break;
case 3:
removeBook();
break;
case 4:
issueBook();
break;
case 5:
issueBookList();
break;
default:
printf("Invalid choice...!\n\n");
}
printf("Press any key to continue...\n");
// getch();
fflush(stdin);
getchar();
// getchar_unlocked();
} while(1);
return 0;
}
void addBook() {
char myDate[12];
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(myDate, "%02d/%02d/%d", tm.tm_mday, tm.tm_mon+1, tm.tm_year + 1900);
strcpy(bk.date, myDate);
fp = fopen("book_data.txt", "ab");
printf("Enter book id: ");
scanf("%d", &bk.id);
printf("Enter book name: ");
fflush(stdin);
// fgets(bk.bookName, 50, stdin);
scanf("%s", bk.bookName);
printf("Enter author name (use 'unknown' if not known): ");
fflush(stdin);
// fgets(bk.authorName, 50, stdin);
scanf("%s", bk.authorName);
printf("Book Added Successfully...\n");
fwrite(&bk, sizeof(bk), 1, fp);
fclose(fp);
}
void bookList() {
system("clear");
printf("<== Available Books ==>\n\n");
printf("%-10s %-30s %-20s %s\n\n", "Book id", "Book Name", "Author", "Date");
fp = fopen("book_data.txt", "rb");
while(fread(&bk, sizeof(bk), 1, fp) == 1) {
printf("%-10d %-30s %-20s %s\n\n", bk.id, bk.bookName, bk.authorName, bk.date);
}
fclose(fp);
}
void removeBook() {
int id;
int f=0;
system("clear");
printf("<== Remove Books ==>\n\n");
printf("Enter Book id to remove: ");
scanf("%d", &id);
FILE *ft;
fp = fopen("book_data.txt", "rb");
ft = fopen("book_data.txt", "wb");
while(fread(&bk, sizeof(bk), 1, fp) == 1) {
if(id == bk.id)
f=1;
else
fwrite(&bk, sizeof(bk), 1, ft);
}
if(f == 1)
printf("\n\nDelete Successful.");
else
printf("\n\nRecord not found !");
fclose(fp);
fclose(ft);
remove("book_data.txt");
rename("temp.txt", "book_data.txt");
}
void issueBook() {
char myDate[12];
time_t t = time(NULL);
struct tm tm = *localtime(&t);
sprintf(myDate, "%02d/%02d/%d", tm.tm_mday, tm.tm_mon+1, tm.tm_year + 1900);
strcpy(std.date, myDate);
int f=0;
system("clear");
printf("<== Issue Books ==>\n\n");
printf("Enter Book id to issue: ");
scanf("%d", &std.id);
fp = fopen("book_data.txt", "rb");
while(fread(&bk, sizeof(bk), 1, fp) == 1) {
if(bk.id == std.id) {
strcpy(std.bookName, bk.bookName);
f=1;
break;
}
}
if(f == 0) {
printf("No book found with yhis id\n");
printf("Please try again...\n\n");
return;
}
fp = fopen("issue.txt", "ab");
printf("Enter student name: ");
fflush(stdin);
scanf("%s", std.studentName);
printf("Enter student course: ");
fflush(stdin);
scanf("%s", std.courseName);
printf("Enter student roll no: ");
fflush(stdin);
scanf("%d", &std.studentRollNo);
printf("Book Issued Successfully.\n\n");
fwrite(&std, sizeof(std), 1, fp);
fclose(fp);
}
void issueBookList() {
system("clear");
printf("<== Book Issue List ==>\n\n");
printf("%-10s %-30s %-20s %-10s %-30s %s\n\n", "Std.id", "Name", "Course", "Rollno", "Book Name", "Date");
fp = fopen("issue.txt", "rb");
while(fread(&std, sizeof(std), 1, fp) == 1) {
printf("%-10d %-30s %-20s %-10d %-30s %s\n", std.id, std.studentName, std.courseName, std.studentRollNo, std.bookName, std.date);
}
fclose(fp);
}