-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiplex.c
731 lines (624 loc) · 23.5 KB
/
multiplex.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define MAX_NAME_LENGTH 50
#define MAX_SEATS 30
#define MOVIE_FILENAME "movies.txt"
#define ADMIN_FILENAME "admin.txt"
#define FILENAME "booking_list.txt"
#define TODAY_MOVIES_FILENAME "movies_today.txt"
#define LAST_SELECTED_DATE_FILENAME "last_selected_date.txt"
#define MAX_MOVIES 30
int MAX_SELECTED_MOVIES=3;
typedef enum { FRONT_CIRCLE, REAR_CIRCLE } Category;
typedef struct {
int id;
char name[MAX_NAME_LENGTH];
int age;
Category category;
char movie[MAX_NAME_LENGTH];
} Ticket;
typedef struct {
char title[MAX_NAME_LENGTH];
char director[MAX_NAME_LENGTH];
int year;
} MovieInfo;
typedef struct Node {
Ticket ticket;
struct Node* next;
} Node;
Node* head = NULL;
// Function prototypes
void clearInputBuffer();
void saveToFile(const char *filename);
void loadFromFile(const char *filename);
void bookTicket(const char selectedMovies[][MAX_NAME_LENGTH], int numSelectedMovies);
void inquireSeat();
int countTickets();
void loadMoviesFromFile(const char *filename, MovieInfo movies[], int *numMovies);
void adminLogin(char username[], char password[]);
void selectMoviesForToday(MovieInfo movies[], int numMovies, char selectedMovies[][MAX_NAME_LENGTH], int numSelectedMovies);
void displaySelectedMovies(const char selectedMovies[][MAX_NAME_LENGTH], int numSelectedMovies);
void movieBookingSystem();
void adminPanel(MovieInfo movies[], int *numMovies, char selectedMovies[][MAX_NAME_LENGTH], bool *moviesSelected);
void addMovieFromCatalog(MovieInfo movies[], int numMovies, char selectedMovies[][MAX_NAME_LENGTH]);
void addNewMovie(MovieInfo movies[], int *numMovies, char selectedMovies[][MAX_NAME_LENGTH]);
void deleteMovie(MovieInfo movies[], int *numMovies, char selectedMovies[][MAX_NAME_LENGTH], bool *moviesSelected);
void changeMovieSelection(MovieInfo movies[], int numMovies, char selectedMovies[][MAX_NAME_LENGTH], bool *moviesSelected);
void saveLastSelectedDate(time_t date);
void loadLastSelectedDate(time_t *date);
bool isSameDate(time_t date1, time_t date2);
void updateMoviesForToday(const char *movieFilename, const char *todayMovieFilename);
void checkAndUpdateMoviesForToday();
void saveTicketToFile( const Ticket *ticket);
int main() {
// Check and update movies for today
checkAndUpdateMoviesForToday();
int c;
c = countTickets();
printf("%d",c);
// Start movie booking system
movieBookingSystem();
return 0;
}
void clearInputBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
void saveToFile(const char *filename) {
FILE* file = fopen(filename, "a+");
if (file == NULL) {
printf("Error opening file %s for writing!\n", filename);
return;
}
Node* current = head;
while (current != NULL) {
fprintf(file, "%d,%s,%d,%d,%s\n", current->ticket.id, current->ticket.name, current->ticket.age, current->ticket.category, current->ticket.movie);
current = current->next;
}
fclose(file);
}
void loadFromFile(const char *filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file %s for reading!\n", filename);
return;
}
Ticket ticket;
while (fscanf(file, "%d,%[^,],%d,%d,%[^\n]", &ticket.id, ticket.name, &ticket.age, &ticket.category, ticket.movie) == 5) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("Memory allocation failed!\n");
fclose(file);
exit(EXIT_FAILURE);
}
newNode->ticket = ticket;
newNode->next = head;
head = newNode;
}
fclose(file);
}
void bookTicket(const char selectedMovies[][MAX_NAME_LENGTH], int numSelectedMovies)
{
int totalTickets = countTickets();
if (totalTickets >= MAX_SEATS)
{
printf("Booking is full!\n");
return;
}
else if (totalTickets >= MAX_SEATS / 2)
{
printf("Senior citizen preference!\n");
int age;
printf("Enter your age: ");
if (scanf("%d", &age) != 1 || age < 60)
{
printf("Sorry, only senior citizens are allowed to book at this time.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
Category category = FRONT_CIRCLE; // Senior citizen, assign to front circle
int index;
printf("Enter the index of the movie you want to watch: ");
if (scanf("%d", &index) != 1 || index < 1 || index > numSelectedMovies)
{
printf("Invalid input! Please enter a valid index.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
int id;
printf("Enter an ID:\n");
if (scanf("%d", &id) != 1) {
printf("Invalid ID! Please enter a valid integer.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
char name[MAX_NAME_LENGTH];
printf("Enter your name: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character
Ticket ticket;
ticket.id = id; // Generate a random ID
strcpy(ticket.name, name);
ticket.age = age;
ticket.category = category;
strcpy(ticket.movie, selectedMovies[index - 1]);
saveTicketToFile(&ticket);
printf("Ticket booked successfully!\n");
}
else
{
int id;
printf("Enter an ID:\n");
if (scanf("%d", &id) != 1) {
printf("Invalid ID! Please enter a valid integer.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
char name[MAX_NAME_LENGTH];
printf("Enter your name: ");
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character
int age;
printf("Enter your age: ");
if (scanf("%d", &age) != 1 || age <= 0) {
printf("Invalid age! Please enter a positive integer.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
Category category;
printf("Enter your category (0 for Front Circle, 1 for Rear Circle): ");
int cat;
if (scanf("%d", &cat) != 1 || (cat != 0 && cat != 1)) {
printf("Invalid category! Please enter 0 or 1.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
category = (cat == 0) ? FRONT_CIRCLE : REAR_CIRCLE;
int index;
printf("Enter the index of the movie you want to watch: ");
if (scanf("%d", &index) != 1 || index < 1 || index > numSelectedMovies) {
printf("Invalid input! Please enter a valid index.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
Ticket ticket;
ticket.id = id; // Generate a random ID
strcpy(ticket.name, name);
ticket.age = age;
ticket.category = category;
strcpy(ticket.movie, selectedMovies[index - 1]);
saveTicketToFile(&ticket);
printf("Ticket booked successfully!\n");
}
}
void saveTicketToFile(const Ticket *ticket) {
FILE *file = fopen(FILENAME, "a+");
if (file == NULL) {
printf("Error opening file for writing.\n");
return;
}
fprintf(file, "%d,%s,%d,%d,%s\n", ticket->id, ticket->name, ticket->age, ticket->category, ticket->movie);
fclose(file);
}
void inquireSeat() {
FILE* fp = fopen(FILENAME, "r");
if (fp == NULL) {
printf("Error opening file!\n");
return;
}
printf("Enter your name: ");
char name[MAX_NAME_LENGTH];
fgets(name, MAX_NAME_LENGTH, stdin);
name[strcspn(name, "\n")] = '\0'; // Remove newline character
printf("Enter your ID: ");
int id;
scanf("%d", &id);
clearInputBuffer(); // Clear input buffer
Ticket ticket;
bool found = false;
while (fscanf(fp, "%d,%49[^,],%d,%d,%99[^\n]", &ticket.id, ticket.name, &ticket.age, &ticket.category, ticket.movie) == 5) {
if (strcmp(ticket.name, name) == 0 && ticket.id == id) {
found = true;
printf("Ticket found! Details:\n");
printf("Name: %s\n", ticket.name);
printf("ID: %d\n", ticket.id);
printf("Age: %d\n", ticket.age);
printf("Category: %s\n", (ticket.category == FRONT_CIRCLE) ? "Front Circle" : "Rear Circle");
printf("Movie: %s\n", ticket.movie);
break;
}
}
if (!found) {
printf("Ticket not found!\n");
}
fclose(fp);
}
int countTickets() {
FILE *fp = fopen(FILENAME, "r");
if (fp == NULL) {
printf("Error opening file!\n");
return 0;
}
int count = 0;
char line[200];
while (fgets(line, sizeof(line), fp) != NULL) {
count++;
}
fclose(fp);
return count;
}
void loadMoviesFromFile(const char *filename, MovieInfo movies[], int *numMovies) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file %s for reading!\n", filename);
return;
}
int count = 0;
char line[MAX_NAME_LENGTH * 3 + 10]; // Maximum line length assuming MAX_NAME_LENGTH for each field and ',' separators
while (fgets(line, sizeof(line), file) != NULL && count < MAX_MOVIES) {
char title[MAX_NAME_LENGTH], director[MAX_NAME_LENGTH];
int year;
if (sscanf(line, "%[^,],%[^,],%d", title, director, &year) == 3) {
strcpy(movies[count].title, title);
strcpy(movies[count].director, director);
movies[count].year = year;
count++;
} else {
printf("Error parsing line: %s\n", line);
}
}
*numMovies = count;
fclose(file);
}
void adminLogin(char username[], char password[]) {
FILE* file = fopen(ADMIN_FILENAME, "r");
if (file == NULL) {
printf("Error opening admin file for reading!\n");
return;
}
fscanf(file, "%s %s", username, password);
fclose(file);
}
void selectMoviesForToday(MovieInfo movies[], int numMovies, char selectedMovies[][MAX_NAME_LENGTH], int numSelectedMovies)
{
// Check if the last selected date matches today's date
time_t lastSelectedDate, currentDate;
loadLastSelectedDate(&lastSelectedDate);
time(¤tDate);
if (!isSameDate(lastSelectedDate, currentDate))
{
// If dates don't match, select new movies randomly
srand(time(NULL));
int i, j;
MovieInfo tempMovies[MAX_MOVIES]; // Temporary array to store movies
for (i = 0; i < numMovies; i++) {
strcpy(tempMovies[i].title, movies[i].title);
strcpy(tempMovies[i].director, movies[i].director);
tempMovies[i].year = movies[i].year;
}
for (i = 0; i < numSelectedMovies; i++) {
int index = rand() % numMovies;
strcpy(selectedMovies[i], tempMovies[index].title);
// Remove the selected movie from the list to prevent duplicates
for (j = index; j < numMovies - 1; j++) {
strcpy(tempMovies[j].title, tempMovies[j + 1].title);
strcpy(tempMovies[j].director, tempMovies[j + 1].director);
tempMovies[j].year = tempMovies[j + 1].year;
}
numMovies--;
}
// Save the current date as the last selected date
saveLastSelectedDate(currentDate);
// Save selected movies to movies_today.txt
FILE *file = fopen(TODAY_MOVIES_FILENAME, "w");
if (file == NULL) {
printf("Error opening file %s for writing!\n", TODAY_MOVIES_FILENAME);
return;
}
for (int i = 0; i < numSelectedMovies; i++) {
fprintf(file, "%s\n", selectedMovies[i]);
}
fclose(file);
// Debug output
printf("Selected movies for today:\n");
for (int i = 0; i < numSelectedMovies; i++) {
printf("%d. %s\n", i + 1, selectedMovies[i]);
}
} else {
// If dates match, load selected movies from movies_today.txt
FILE *file = fopen(TODAY_MOVIES_FILENAME, "r");
if (file == NULL) {
// Create the file if it doesn't exist
file = fopen(TODAY_MOVIES_FILENAME, "w");
if (file == NULL) {
printf("Error creating file %s!\n", TODAY_MOVIES_FILENAME);
return;
}
fclose(file);
return; // No movies selected yet, return
}
for (int i = 0; i < numSelectedMovies; i++) {
fgets(selectedMovies[i], MAX_NAME_LENGTH, file);
selectedMovies[i][strcspn(selectedMovies[i], "\n")] = 0; // Remove newline character
}
fclose(file);
// Debug output
printf("Selected movies for today:\n");
for (int i = 0; i < numSelectedMovies; i++) {
printf("%d. %s\n", i + 1, selectedMovies[i]);
}
}
}
void displaySelectedMovies(const char selectedMovies[][MAX_NAME_LENGTH], int numSelectedMovies) {
printf("Movies screened for today:\n");
for (int i = 0; i < numSelectedMovies; i++) {
printf("%d. %s\n", i + 1, selectedMovies[i]);
}
}
void movieBookingSystem() {
char adminUsername[MAX_NAME_LENGTH];
char adminPassword[MAX_NAME_LENGTH];
adminLogin(adminUsername, adminPassword);
char enteredUsername[MAX_NAME_LENGTH];
char enteredPassword[MAX_NAME_LENGTH];
bool moviesSelected = false;
char selectedMovies[MAX_SELECTED_MOVIES][MAX_NAME_LENGTH];
// Load movies
MovieInfo movies[MAX_MOVIES];
int numMovies = 0;
loadMoviesFromFile(MOVIE_FILENAME, movies, &numMovies);
// Select movies for today if not already selected
if (!moviesSelected) {
selectMoviesForToday(movies, numMovies, selectedMovies, MAX_SELECTED_MOVIES);
moviesSelected = true;
}
while (1) {
printf("\nWelcome to Movie Ticket Booking System\n");
printf("Movies for today:\n");
displaySelectedMovies(selectedMovies, MAX_SELECTED_MOVIES); // Display movies for today
printf("1. Book a ticket\n");
printf("2. Inquire about seat\n");
printf("3. Admin Login\n");
printf("4. Exit\n");
printf("Enter your choice: ");
int choice;
if (scanf("%d", &choice) != 1) {
printf("Invalid input! Please enter a number.\n");
clearInputBuffer();
continue;
}
clearInputBuffer();
switch (choice) {
case 1:
if (moviesSelected)
bookTicket(selectedMovies, MAX_SELECTED_MOVIES);
else
printf("Movies for today are not selected yet. Please contact admin.\n");
break;
case 2:
inquireSeat();
break;
case 3:
printf("Enter Admin Username: ");
fgets(enteredUsername, MAX_NAME_LENGTH, stdin);
enteredUsername[strcspn(enteredUsername, "\n")] = 0; // Remove newline character
printf("Enter Admin Password: ");
fgets(enteredPassword, MAX_NAME_LENGTH, stdin);
enteredPassword[strcspn(enteredPassword, "\n")] = 0; // Remove newline character
if (strcmp(enteredUsername, adminUsername) == 0 && strcmp(enteredPassword, adminPassword) == 0) {
adminPanel(movies, &numMovies, selectedMovies, &moviesSelected);
} else {
printf("Invalid credentials! Access denied.\n");
}
break;
case 4:
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
}
void adminPanel(MovieInfo movies[], int *numMovies, char selectedMovies[][MAX_NAME_LENGTH], bool *moviesSelected) {
while (1) {
printf("\nAdmin Panel\n");
printf("1. Add a movie from catalog\n");
printf("2. Add a new movie\n");
printf("3. Delete a movie\n");
printf("4. Change movie selection for today\n");
printf("5. Back to main menu\n");
printf("Enter your choice: ");
int choice;
if (scanf("%d", &choice) != 1) {
printf("Invalid input! Please enter a number.\n");
clearInputBuffer();
continue;
}
clearInputBuffer();
switch (choice) {
case 1:
addMovieFromCatalog(movies, *numMovies, selectedMovies);
break;
case 2:
addNewMovie(movies, numMovies, selectedMovies);
break;
case 3:
deleteMovie(movies, numMovies, selectedMovies, moviesSelected);
break;
case 4:
changeMovieSelection(movies, *numMovies, selectedMovies, moviesSelected);
break;
case 5:
return;
default:
printf("Invalid choice! Please try again.\n");
}
}
}
void addMovieFromCatalog(MovieInfo movies[], int numMovies, char selectedMovies[][MAX_NAME_LENGTH]) {
printf("Catalog Movies:\n");
for (int i = 0; i < numMovies; i++) {
printf("%d. %s\n", i + 1, movies[i].title);
}
printf("Enter the index of the movie to add: ");
int index;
if (scanf("%d", &index) != 1 || index < 1 || index > numMovies) {
printf("Invalid input! Please enter a valid index.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
if (index < 1 || index > numMovies) {
printf("Invalid index! Please try again.\n");
return;
}
// Check if the movie is already selected for today
for (int i = 0; i < MAX_SELECTED_MOVIES; i++) {
if (strcmp(selectedMovies[i], movies[index - 1].title) == 0) {
printf("Movie is already selected for today!\n");
return;
}
}
// Add the selected movie to today's movies
for (int i = 0; i < MAX_SELECTED_MOVIES; i++) {
if (selectedMovies[i][0] == '\0') {
strcpy(selectedMovies[i], movies[index - 1].title);
printf("Movie added successfully!\n");
return;
}
}
printf("Maximum movies selected for today!\n");
}
void addNewMovie(MovieInfo movies[], int *numMovies, char selectedMovies[][MAX_NAME_LENGTH]) {
if (*numMovies >= MAX_MOVIES) {
printf("Maximum number of movies reached!\n");
return;
}
printf("Enter movie title: ");
fgets(movies[*numMovies].title, MAX_NAME_LENGTH, stdin);
movies[*numMovies].title[strcspn(movies[*numMovies].title, "\n")] = 0; // Remove newline character
printf("Enter movie director: ");
fgets(movies[*numMovies].director, MAX_NAME_LENGTH, stdin);
movies[*numMovies].director[strcspn(movies[*numMovies].director, "\n")] = 0; // Remove newline character
printf("Enter release year: ");
if (scanf("%d", &movies[*numMovies].year) != 1) {
printf("Invalid input! Please enter a valid year.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
// Add the movie to today's movies if not already selected
for (int i = 0; i < MAX_SELECTED_MOVIES; i++) {
if (selectedMovies[i][0] == '\0') {
strcpy(selectedMovies[i], movies[*numMovies].title);
break;
}
}
(*numMovies)++;
printf("New movie added successfully!\n");
}
void deleteMovie(MovieInfo movies[], int *numMovies, char selectedMovies[][MAX_NAME_LENGTH], bool *moviesSelected) {
if (*numMovies == 0) {
printf("No movies available to delete!\n");
return;
}
printf("Movies:\n");
for (int i = 0; i < *numMovies; i++) {
printf("%d. %s\n", i + 1, movies[i].title);
}
printf("Enter the index of the movie to delete: ");
int index;
if (scanf("%d", &index) != 1 || index < 1 || index > *numMovies) {
printf("Invalid input! Please enter a valid index.\n");
clearInputBuffer();
return;
}
clearInputBuffer();
// Delete the movie from today's movies if selected
for (int i = 0; i < MAX_SELECTED_MOVIES; i++) {
if (strcmp(selectedMovies[i], movies[index - 1].title) == 0) {
selectedMovies[i][0] = '\0';
break;
}
}
// Shift remaining movies in the array to cover the deleted movie
for (int i = index - 1; i < *numMovies - 1; i++) {
strcpy(movies[i].title, movies[i + 1].title);
strcpy(movies[i].director, movies[i + 1].director);
movies[i].year = movies[i + 1].year;
}
(*numMovies)--;
printf("Movie deleted successfully!\n");
// Update movies for today if needed
if (*moviesSelected) {
selectMoviesForToday(movies, *numMovies, selectedMovies, MAX_SELECTED_MOVIES);
}
}
void changeMovieSelection(MovieInfo movies[], int numMovies, char selectedMovies[][MAX_NAME_LENGTH], bool *moviesSelected) {
if (numMovies == 0) {
printf("No movies available to select!\n");
return;
}
// Select new movies for today
selectMoviesForToday(movies, numMovies, selectedMovies, MAX_SELECTED_MOVIES);
*moviesSelected = true;
}
void saveLastSelectedDate(time_t date) {
FILE *file = fopen(LAST_SELECTED_DATE_FILENAME, "w");
if (file == NULL) {
printf("Error opening file %s for writing!\n", LAST_SELECTED_DATE_FILENAME);
return;
}
fprintf(file, "%ld", date);
fclose(file);
}
void loadLastSelectedDate(time_t *date) {
FILE *file = fopen(LAST_SELECTED_DATE_FILENAME, "r");
if (file == NULL) {
// Create the file if it doesn't exist
file = fopen(LAST_SELECTED_DATE_FILENAME, "w");
if (file == NULL) {
printf("Error creating file %s!\n", LAST_SELECTED_DATE_FILENAME);
return;
}
fclose(file);
*date = -1;
return;
}
fscanf(file, "%ld", date);
fclose(file);
}
bool isSameDate(time_t date1, time_t date2) {
struct tm* tm1 = localtime(&date1);
struct tm* tm2 = localtime(&date2);
return tm1->tm_mday == tm2->tm_mday &&
tm1->tm_mon == tm2->tm_mon &&
tm1->tm_year == tm2->tm_year;
}
void updateMoviesForToday(const char *movieFilename, const char *todayMovieFilename) {
// Load movies
MovieInfo movies[MAX_MOVIES];
int numMovies = 0;
loadMoviesFromFile(movieFilename, movies, &numMovies);
// Select movies for today
char selectedMovies[MAX_SELECTED_MOVIES][MAX_NAME_LENGTH];
selectMoviesForToday(movies, numMovies, selectedMovies, MAX_SELECTED_MOVIES);
}
void checkAndUpdateMoviesForToday() {
time_t lastSelectedDate, currentDate;
loadLastSelectedDate(&lastSelectedDate);
time(¤tDate);
if (!isSameDate(lastSelectedDate, currentDate)) {
// Update movies for today if the last selected date is not today
updateMoviesForToday(MOVIE_FILENAME, TODAY_MOVIES_FILENAME);
}
}