-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
371 lines (318 loc) · 8.76 KB
/
main.c
File metadata and controls
371 lines (318 loc) · 8.76 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Structures
struct info {
int id;
char name[30];
char origin[30];
char dest[30];
char time[30];
char date[30];
} u;
struct access {
char uname[10];
char pass[20];
} s;
// Function Prototypes
void login();
void create();
void insert();
void display();
void search();
void cancel();
void update();
int avlid(int n);
int emptychecking();
void append();
void menu();
// Function Definitions
void login() {
FILE *fp, *fp_1;
char temp[20];
fflush(stdin);
printf("Enter your username: ");
fgets(s.uname, sizeof(s.uname), stdin);
s.uname[strcspn(s.uname, "\n")] = 0; // Remove trailing newline
if ((fp = fopen(s.uname, "r")) == NULL) {
printf("You're not registered.\nCreate an account.\n");
exit(1);
} else {
fp_1 = fopen("pass.txt", "r");
if (!fp_1) {
printf("Error reading password file.\n");
fclose(fp);
exit(1);
}
printf("Enter your password: ");
fgets(s.pass, sizeof(s.pass), stdin);
s.pass[strcspn(s.pass, "\n")] = 0;
fscanf(fp_1, "%s", temp);
if (strcmp(s.pass, temp) != 0) {
printf("Wrong password.\n");
fclose(fp);
fclose(fp_1);
exit(1);
}
}
fclose(fp);
fclose(fp_1);
printf("Login successful.\n");
}
void create() {
FILE *fp, *fp_1;
fflush(stdin);
printf("Enter your username: ");
fgets(s.uname, sizeof(s.uname), stdin);
s.uname[strcspn(s.uname, "\n")] = 0;
if ((fp = fopen(s.uname, "w")) == NULL) {
printf("Error creating account.\n");
exit(1);
}
fp_1 = fopen("pass.txt", "w");
if (!fp_1) {
printf("Error creating password file.\n");
fclose(fp);
exit(1);
}
printf("Enter password: ");
fgets(s.pass, sizeof(s.pass), stdin);
s.pass[strcspn(s.pass, "\n")] = 0;
fprintf(fp_1, "%s", s.pass);
fclose(fp);
fclose(fp_1);
printf("Account created successfully.\n");
}
void insert() {
FILE *fp = fopen("BUS", "w");
if (!fp) {
printf("Error opening file for writing.\n");
return;
}
fflush(stdin);
printf("Enter National ID: ");
scanf("%d", &u.id);
getchar(); // Clear newline
printf("Enter Name: ");
fgets(u.name, sizeof(u.name), stdin);
u.name[strcspn(u.name, "\n")] = 0;
printf("\n1.Dhaka\t2.Chittagong\n3.Barisal\t4.Khulna\n5.Mymensingh\t6.Rajshahi\n7.Rangpur\t8.Sylhet\n");
printf("Enter Origin From Above: ");
fgets(u.origin, sizeof(u.origin), stdin);
u.origin[strcspn(u.origin, "\n")] = 0;
printf("Select Destination From Above: ");
fgets(u.dest, sizeof(u.dest), stdin);
u.dest[strcspn(u.dest, "\n")] = 0;
printf("Enter Journey Date: ");
fgets(u.date, sizeof(u.date), stdin);
u.date[strcspn(u.date, "\n")] = 0;
printf("Enter Departure Time: ");
fgets(u.time, sizeof(u.time), stdin);
u.time[strcspn(u.time, "\n")] = 0;
fwrite(&u, sizeof(u), 1, fp);
fclose(fp);
printf("Ticket inserted successfully.\n");
}
void display() {
FILE *fp = fopen("BUS", "r");
if (!fp) {
printf("Error opening file for reading.\n");
return;
}
while (fread(&u, sizeof(u), 1, fp)) {
printf("\nID: %d\nName: %s\nOrigin: %s\nDestination: %s\nJourney Date: %s\nDeparture Time: %s\n",
u.id, u.name, u.origin, u.dest, u.date, u.time);
}
fclose(fp);
}
void search() {
int id;
printf("Enter ID to search: ");
scanf("%d", &id);
if (avlid(id) == 0) {
printf("ID %d is not available.\n", id);
return;
}
FILE *fp = fopen("BUS", "r");
if (!fp) {
printf("Error opening file.\n");
return;
}
while (fread(&u, sizeof(u), 1, fp)) {
if (u.id == id) {
printf("\nID: %d\nName: %s\nOrigin: %s\nDestination: %s\nJourney Date: %s\nDeparture Time: %s\n",
u.id, u.name, u.origin, u.dest, u.date, u.time);
break;
}
}
fclose(fp);
}
void cancel() {
int id;
printf("Enter the ID you want to delete: ");
scanf("%d", &id);
if (avlid(id) == 0) {
printf("ID %d is not available in the record.\n", id);
return;
}
FILE *fp = fopen("BUS", "r");
FILE *temp = fopen("temp", "w");
if (!fp || !temp) {
printf("Error opening file.\n");
return;
}
while (fread(&u, sizeof(u), 1, fp)) {
if (u.id != id) {
fwrite(&u, sizeof(u), 1, temp);
}
}
fclose(fp);
fclose(temp);
remove("BUS");
rename("temp", "BUS");
printf("Ticket canceled successfully.\n");
}
void update() {
int id, choice;
printf("Enter the ID to update: ");
scanf("%d", &id);
if (avlid(id) == 0) {
printf("ID %d is not available.\n", id);
return;
}
FILE *fp = fopen("BUS", "r");
FILE *temp = fopen("temp", "w");
if (!fp || !temp) {
printf("Error opening file.\n");
return;
}
while (fread(&u, sizeof(u), 1, fp)) {
if (u.id == id) {
printf("\n1. Update Origin\n2. Update Destination\n3. Update Date\n4. Update Time\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
printf("Enter new Origin: ");
fgets(u.origin, sizeof(u.origin), stdin);
u.origin[strcspn(u.origin, "\n")] = 0;
break;
case 2:
printf("Enter new Destination: ");
fgets(u.dest, sizeof(u.dest), stdin);
u.dest[strcspn(u.dest, "\n")] = 0;
break;
case 3:
printf("Enter new Date: ");
fgets(u.date, sizeof(u.date), stdin);
u.date[strcspn(u.date, "\n")] = 0;
break;
case 4:
printf("Enter new Time: ");
fgets(u.time, sizeof(u.time), stdin);
u.time[strcspn(u.time, "\n")] = 0;
break;
default:
printf("Invalid choice.\n");
break;
}
}
fwrite(&u, sizeof(u), 1, temp);
}
fclose(fp);
fclose(temp);
remove("BUS");
rename("temp", "BUS");
printf("Record updated successfully.\n");
}
int avlid(int n) {
FILE *fp = fopen("BUS", "r");
if (!fp) {
return 0;
}
while (fread(&u, sizeof(u), 1, fp)) {
if (u.id == n) {
fclose(fp);
return 1;
}
}
fclose(fp);
return 0;
}
void menu() {
int choice;
do {
printf("\n1. Insert\n2. Display\n3. Search\n4. Cancel\n5. Update\n6. Append\n7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: insert(); break;
case 2: display(); break;
case 3: search(); break;
case 4: cancel(); break;
case 5: update(); break;
case 6: append(); break;
case 7: exit(0);
default: printf("Invalid choice. Try again.\n"); break;
}
} while (choice != 7);
}
int emptychecking() {
FILE *fp = fopen("BUS", "r");
if (!fp || fgetc(fp) == EOF) {
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}
void append() {
FILE *fp = fopen("BUS", "a");
if (!fp) {
printf("Error opening file for appending.\n");
return;
}
printf("Enter National ID: ");
scanf("%d", &u.id);
getchar(); // Clear newline
printf("Enter Name: ");
fgets(u.name, sizeof(u.name), stdin);
u.name[strcspn(u.name, "\n")] = 0;
printf("Enter Origin: ");
fgets(u.origin, sizeof(u.origin), stdin);
u.origin[strcspn(u.origin, "\n")] = 0;
printf("Enter Destination: ");
fgets(u.dest, sizeof(u.dest), stdin);
u.dest[strcspn(u.dest, "\n")] = 0;
printf("Enter Journey Date: ");
fgets(u.date, sizeof(u.date), stdin);
u.date[strcspn(u.date, "\n")] = 0;
printf("Enter Departure Time: ");
fgets(u.time, sizeof(u.time), stdin);
u.time[strcspn(u.time, "\n")] = 0;
fwrite(&u, sizeof(u), 1, fp);
fclose(fp);
printf("Data appended successfully.\n");
}
// Main function
int main() {
int choice;
printf("\n1. Login\n2. Create Account\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
login();
menu();
break;
case 2:
create();
menu();
break;
default:
printf("Invalid choice. Exiting.\n");
exit(1);
}
return 0;
}