-
Notifications
You must be signed in to change notification settings - Fork 1
/
Data_manage.c
963 lines (903 loc) · 30 KB
/
Data_manage.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
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX 50 // MAX is initialized with 50
enum branch {CSE, EE, ME}; //branch is a enum which can take one of these 3 values . can be changed to have more values
struct course
{
char code[MAX]; // Its a character array of length MAX used to store the course code
char name[MAX]; // Its a character array of length MAX used to store the course name
int credits; // Stores the course credits
int ninstructors; //Stores the total instructors teaching in that course
char **instructor_ids; //2-d dynamically allocated array which stores the id's of the instructors teaching in that course
int nstudents; //Stores the number of students taking that course
char **student_rollnos; // 2-d dynamically allocated array storing roll number of students taking that course
};
struct student
{
char rollno[MAX]; // Its a character array of length MAX used to store the roll number
char name[MAX]; //Another char array of length MAX which stores name of student
enum branch branch; //enum which take a value from {CSE, EE, ME}
int semester; //Stores the semester number for each student
int ncourses; //Stores the total number of courses taken by a student
char **courses_codes; //2-d dynamically initialized character vector storing codes for all courses taken
};
struct instructor
{
char id[MAX]; //Character array of length MAX which stores id of that instructor
char name[MAX]; //Character array of length MAX 50 which stores name of that instructor
int ncourses; //Stores the number of courses taught by that instructor
char **courses_codes; // 2-d dynamically allocated array storing course codes of courses taught by that instructor
};
void print_students(); //Prints all student details
void print_student(struct student s); //Prints the details of the input student
void add_student(struct student s); //Adds the input student if it's not already there.
void add_new_student(); //Adds a new student with predefined values
struct student modify_student(struct student s); //Modifies the details of a student
void add_instructors(struct instructor ins); //Adds a new instructor if its not already there
void print_instructor(struct instructor ins); //Prints the details of that instructor
void print_instructors(); //Prints details of all instructors
void add_new_instructor(); //Adds a new instructor with predefined initail values
struct instructor modify_instructor(struct instructor ins); //Modifies details of the input instructor
void add_course(struct course c); //Adds a new course.
void print_course(struct course c); //Prints the details of the input course.
void print_courses(); //Prints details of all courses present.
void add_new_course(); //Adds a new course with predefined values
struct course modify_course(struct course c); //Modifies the details of input course
void load_records(); //Loads all data from txt files
void load_students(); //Loads the student data from students.txt
void load_instructors(); //Loads the instructor data from instructors.txt
void load_courses(); //Loads the courses data from courses.txt
void write_records(); //Writes all data to txt files
void write_students(); //Writes students data to students.txt file
void write_instructors(); //Writes instructors data to instructors.txt file
void write_courses(); //Writes courses data to courses.txt file
struct course *courses;
struct student *students;
struct instructor *instructors;
FILE *fps;
FILE *fpc;
FILE *fpi;
int ncourses = 0, nstudents = 0, ninstructors = 0;
int main()
{
int n,m,check=0;
struct student s;
struct course c;
struct instructor ins;
char ch;
printf("\nProgram To manage details...\nCurrently Saved in Records/Files : \n");
load_records();
printf("Kindly enter only 1 word for any option \n");
printf("\nEnter 1) for Student options");
printf("\nEnter 2) for Instructor options");
printf("\nEnter 3) for Course options");
printf("\nEnter your choice from above :");
scanf("%d",&n);
switch(n) //We use switch statements to have a decent graphical interface.
{ case 1:
{printf("\n1)Display details of all students");
printf("\n2)Display detail of student of roll no");
printf("\n3)Add details of a new student");
printf("\n4)Delete details of student of roll no");
printf("\n5)Modify details of Existing student");
printf("\nEnter your choice from above : ");
scanf("%d",&m);
switch(m)
{
case 1:
{print_students();
break;
}
case 2:
{ printf("Enter the roll no :");
scanf("%s",s.rollno);
for(int i=0;i<nstudents;i++)
{if (strcmp(students[i].rollno,s.rollno)==0)
{s=students[i];
check=1;
}
}
if(check==0)
{printf("Can't fint that roll no\n");
break;
}
else print_student(s);
break;
}
case 3:
{ add_new_student();
break;
}
case 4:
{printf("Enter the roll no :");
scanf("%s",s.rollno);
for(int i=0;i<nstudents;i++)
{if (strcmp(students[i].rollno,s.rollno)==0)
{ int j=i;
for( ;j<nstudents-1;j++)
students[j]=students[j+1];
nstudents--;
printf("\nDeletion success \n");
check=1;
break;
}
}
if(check==0)
printf("Can't fint that roll no\n");
break;
}
case 5:
{printf("Enter the roll no :");
scanf("%s",s.rollno);
for(int i=0;i<nstudents;i++)
{ if (strcmp(students[i].rollno,s.rollno)==0)
{students[i]=modify_student(students[i]);
check=1;
printf("Details changed successfully\n");
scanf("%c",ch);
break;
}
}
if(check==0)
printf("Can't fint that roll no\n");
break;
}
default :
printf("Entered choice is not correct\n");
} break;
}
case 2:
{ printf("\n1)Display details of all instructors");
printf("\n2)Display detail of instructor of id");
printf("\n3)Add details of a new instructor");
printf("\n4)Delete details of instructor of id");
printf("\n5)Modify details of Existing instructor");
printf("\nEnter your choice from above : ");
scanf("%d",&m);
switch(m)
{
case 1:
{print_instructors();
break;
}
case 2:
{printf("Enter the id :");
scanf("%s",ins.id);
for(int i=0;i<ninstructors;i++)
{if (strcmp(instructors[i].id,ins.id)==0)
{ins=instructors[i];
check=1;
}
}
if(check==0)
{printf("Can't fint that id\n");
break;
}
else print_instructor(ins);
break;
}
case 3:
{ add_new_instructor();
break;
}
case 4:
{printf("Enter the instructor id :");
scanf("%s",ins.id);
for(int i=0;i<ninstructors;i++)
{if (strcmp(instructors[i].id,ins.id)==0)
{ int j=i;
for( ;j<ninstructors-1;j++)
instructors[j]=instructors[j+1];
ninstructors--;
printf("Deletion success \n");
check=1;
break;
}
}
if(check==0)
printf("Can't fint that instructor id\n");
break;
}
case 5:
{ printf("Enter the instructor id :");
scanf("%s",ins.id);
for(int i=0;i<ninstructors;i++)
{if (strcmp(instructors[i].id,ins.id)==0)
{instructors[i]=modify_instructor(instructors[i]);
check=1;
printf("Details changed successfully\n") ;
break;
}
}
if(check==0)
{printf("Can't fint that instructor id\n");
}
break;
}
default :
printf("Entered choice is not correct\n");
}
break;
}
case 3:
{ printf("\n1)Display details of all courses");
printf("\n2)Display detail of a course by its code");
printf("\n3)Add details of a new course");
printf("\n4)Delete details of a course by code");
printf("\n5)Modify details of Existing course");
printf("\nEnter your choice from above : ");
scanf("%d",&m);
switch(m)
{
case 1:
{print_courses();
break;
}
case 2:
{ printf("Enter the course code :");
scanf("%s",c.code);
for(int i=0;i<ncourses;i++)
{if (strcmp(courses[i].code,c.code)==0)
{print_course(courses[i]);
check=1;
}
}
if(check==0)
{printf("Can't fint that course code \n");
break;
}
}
case 3:
{ add_new_course();
break;
}
case 4:
{printf("Enter the course code:");
scanf("%s",c.code);
for(int i=0;i<ncourses;i++)
{if (strcmp(courses[i].code,c.code)==0)
{ int j=i;
for( ;j<ncourses-1;j++)
courses[j]=courses[j+1];
ncourses--;
printf("Deletion success\n ");
check=1;
break;
}
}
if(check==0)
printf("Can't fint that course code\n");
break;
}
case 5:
{ printf("Enter the course code :");
scanf("%s",c.code);
for(int i=0;i<ncourses;i++)
{if (strcmp(courses[i].code,c.code)==0)
{courses[i]=modify_course(courses[i]);
check=1;
printf("Details changed successfully\n");
break;
}
}
if(check==0)
{printf("Can't fint that course code\n");
break;
}
break;
}
default :
printf("Entered choice is not correct\n");
} break;
}
}
write_records();
return 0;
}
void add_student(struct student s)
{
students = realloc (students, (nstudents+1)*sizeof(struct student));
students[nstudents] = s;
nstudents++;
return;
}
void add_instructor(struct instructor ins)
{
instructors = realloc (instructors, (ninstructors+1)*sizeof(struct instructor));
instructors[ninstructors] = ins;
ninstructors++;
return;
}
void add_course(struct course c)
{
courses = realloc (courses, (ncourses+1)*sizeof(struct course));
courses[ncourses] = c;
ncourses++;
return;
}
void add_new_student()
{ char ch;
struct student s;
printf("Enter Roll No:");
scanf("%s",s.rollno);
scanf("%c",ch);
for(int i=0;i<nstudents;i++)
{ if(strcmp(students[i].rollno,s.rollno)==0)
{printf("FAIL! Roll no already Exists \n");
return;
}
}
printf("Enter Name : ");
scanf("%s",s.name);
printf("Enter Branch 1)CSE 2)EE 3)ME :");
scanf("%u",&s.branch);
s.branch-=1;
printf("Enter Semester No:");
scanf("%d", &s.semester);
printf("Enter No of courses :");
scanf("%d", &s.ncourses);
s.courses_codes = malloc (s.ncourses * sizeof(char *));
for (int j=0; j<s.ncourses; j++)
{ int check =0;
s.courses_codes[j] = malloc (MAX * sizeof (char));
printf("Enter course %d :",j+1);
scanf("%s",s.courses_codes[j]);
for(int k=0;k<ncourses;k++)
{if(strcmp(courses[k].code,s.courses_codes[j])==0)
{courses[k].nstudents+=1;
courses[k].student_rollnos = realloc(courses[k].student_rollnos,
courses[k].nstudents * sizeof(char *));
if((courses[k].student_rollnos)==NULL)
{printf("\nERROR Memory didnt reallocate \n");
return;
}
courses[k].student_rollnos[courses[k].nstudents-1] =
malloc (MAX * sizeof (char));
printf("\t entering this rollno in this course \n");
strcpy(courses[k].student_rollnos[courses[k].nstudents-1],s.rollno);
check=1;
}
}
if(check==0)
{struct course c;
strcpy(c.code,s.courses_codes[j]);
strcpy(c.name,"DEFAULT");
c.credits=1;
c.ninstructors=1;
c.instructor_ids = malloc(c.ninstructors * sizeof(char *));
c.instructor_ids[0] = malloc (MAX * sizeof (char));
strcpy(c.instructor_ids[0],"DEFAULT_ins");
c.nstudents=1;
c.student_rollnos = malloc(c.nstudents * sizeof(char *));
c.student_rollnos[0] = malloc (MAX * sizeof (char));
strcpy(c.student_rollnos[0],s.rollno);
printf("\tThis new course code added in courses With DEFAULT values\n");
add_course(c);
}
}
add_student(s);
return;
}
void add_new_instructor()
{ struct instructor ins;
printf("Enter Instructor id:");
scanf("%s",ins.id);
for(int i=0;i<ninstructors;i++)
{ if(strcmp(instructors[i].id,ins.id)==0) //Additional condition to see it it does not already exist
{printf("FAIL! ID already Exists \n");
return;
}
}
printf("Enter Name :");
scanf("%s",ins.name);
printf("Enter number of courses: ");
scanf("%d",&ins.ncourses);
ins.courses_codes = malloc (ins.ncourses * sizeof(char *));
printf("Enter the course codes :\n");
for (int i=0; i<ins.ncourses; i++)
{ printf("Enter course code %d) ",i+1);
ins.courses_codes[i] = malloc (MAX * sizeof (char));
scanf("%s", ins.courses_codes[i]);
int check=0;
for(int k=0;k<ncourses;k++) //To check and add instructor id in course
{ if(strcmp(courses[k].code,ins.courses_codes[i])==0)
{
for(int l=0;l<courses[k].ninstructors;l++)
{if(strcmp(courses[k].instructor_ids[l],ins.id)==0)
check=2;
}
if(check!=2)
{ courses[k].ninstructors +=1;
courses[k].instructor_ids = realloc(courses[k].instructor_ids,
courses[k].ninstructors * sizeof(char *));
if((courses[k].instructor_ids)==NULL)
{printf("\nERROR Memory didnt reallocate \n");
return;
}
courses[k].instructor_ids[courses[k].ninstructors-1] =
malloc (MAX * sizeof (char));
printf("\tThis instructor id entered in this course \n");
strcpy(courses[k].instructor_ids[courses[k].ninstructors-1],ins.id);
check=1;
}
}
}
if(check==0) //To create new course if its not present
{ struct course c;
strcpy(c.code,ins.courses_codes[i]);
strcpy(c.name,"DEFAULT");
c.credits=1;
c.ninstructors=1;
c.instructor_ids = malloc(c.ninstructors * sizeof(char *));
c.instructor_ids[0] = malloc (MAX * sizeof (char));
strcpy(c.instructor_ids[0],ins.id);
c.nstudents=1;
c.student_rollnos = malloc(c.nstudents * sizeof(char *));
c.student_rollnos[0] = malloc (MAX * sizeof (char));
strcpy(c.student_rollnos[0],"DEFAULT_s");
printf("\tThis new course code added in courses With DEFAULT values\n");
add_course(c);
}
}
add_instructor(ins);
return;
}
void add_new_course()
{ struct course c;
printf("Enter course code");
scanf(" %s", c.code);
for(int i=0;i<ncourses;i++)
{ if(strcmp(courses[i].code,c.code)==0) //Additional condition to see if the course already exists.
{printf("FAIL!that course code exists");
return;
}
}
printf("Enter Name of course : ");
scanf("%s",c.name);
printf("Enter total Credits of course : ");
scanf("%d",&c.credits);
printf("Enter number of instructors for that course :");
scanf("%d",&c.ninstructors);
c.instructor_ids = malloc(c.ninstructors * sizeof(char *));
printf("Enter the ID /s of the instructors\n");
for (int i=0; i<c.ninstructors; i++)
{printf("instructor %d) ID ",i+1);
c.instructor_ids[i] = malloc (MAX * sizeof (char));
scanf("%s", c.instructor_ids[i]);
int check=0;
for(int k=0;k<ninstructors;k++) //To check if this course code is present for this instructor
{ if(strcmp(c.instructor_ids[i],instructors[k].id)==0)
{
for(int l=0;l<instructors[k].ncourses;l++)
{if(strcmp(instructors[k].courses_codes[l],c.code)==0)
check=2;
}
if(check!=2)
{instructors[k].ncourses +=1; //When new instructor is added we reallocate the block with more space as that of 1 instructor.
instructors[k].courses_codes = realloc(instructors[k].courses_codes,
instructors[k].ncourses * sizeof(char *));
if((instructors[k].courses_codes)==NULL)
{printf("\nERROR Memory didnt reallocate \n");
return;
}
instructors[k].courses_codes[instructors[k].ncourses-1] =
malloc (MAX * sizeof (char));
printf("\tThis course code entered for this instructor \n");
strcpy(instructors[k].courses_codes[instructors[k].ncourses-1],c.code);
check=1;
}
}
}
if (check==0)
{ struct instructor ins;
strcpy(ins.id,c.instructor_ids[i]);
strcpy(ins.name,"DEFAULT_ins");
ins.ncourses=1;
ins.courses_codes = malloc (ins.ncourses * sizeof(char *));
ins.courses_codes[0] = malloc (MAX * sizeof (char));
strcpy(ins.courses_codes[0],c.code);
printf("\tThis new Instructor added With DEFAULT values\n");
add_instructor(ins);
}
}
printf("Enter of number students in course :");
scanf("%d",&c.nstudents);
c.student_rollnos = malloc(c.nstudents * sizeof(char *));
printf("Enter the roll no of students\n");
for (int i=0; i<c.nstudents; i++)
{ printf("Enter rollno of student %d) ",i+1);
c.student_rollnos[i] = malloc (MAX * sizeof (char));
scanf("%s", c.student_rollnos[i]);
int check=0;
for(int k=0;k<nstudents;k++) //To check if that student has this course
{ if(strcmp(c.student_rollnos[i],students[k].rollno)==0)
{for(int l=0;l<students[k].ncourses;l++)
{if(strcmp(students[k].courses_codes[l],c.code)==0)
check=2;
}
if(check!=2)
{students[k].ncourses +=1;
students[k].courses_codes = realloc(students[k].courses_codes,
students[k].ncourses * sizeof(char *));
if((students[k].courses_codes)==NULL)
{printf("\nERROR Memory didnt reallocate \n");
return;
}
students[k].courses_codes[students[k].ncourses-1] =
malloc (MAX * sizeof (char));
printf("\tThis course added for this roll no \n");
strcpy(students[k].courses_codes[students[k].ncourses-1],c.code);
check=1;
}
}
}
if (check==0) //Add new students with default values ie. CSE branch, 1 course .
{ struct student s;
strcpy(s.rollno,c.student_rollnos[i]);
strcpy(s.name,"DEFAULT_s");
s.branch=0;
s.ncourses=1;
s.courses_codes = malloc (s.ncourses * sizeof(char *));
s.courses_codes[0] = malloc (MAX * sizeof (char));
strcpy(s.courses_codes[0],c.code);
printf("\tThis new Student added With DEFAULT values\n");
add_student(s);
}
}
add_course(c);
return;
}
void print_students()
{
printf("Student's details\n--------------------\n");
for (int i=0; i<nstudents; i++)
{
print_student(students[i]);
}
return;
}
void print_instructors()
{
printf("Instructor's details\n---------------------\n");
for (int i=0; i<ninstructors; i++)
{
print_instructor(instructors[i]);
}
return;
}
void print_courses()
{
printf("Course's details\n------------------------\n");
for (int i=0; i<ncourses; i++)
{
print_course(courses[i]);
}
return;
}
void print_student(struct student s)
{
printf("Roll no.: %s\t\t", s.rollno);
printf("Name: %s\n", s.name);
printf("Branch: ");
if (s.branch==0)
printf(" C S");
else if (s.branch==1)
printf(" E E");
else if(s.branch==2)
printf(" M E");
printf("\t\tSemester: %d\n", s.semester);
printf("List of courses:\n");
for (int i=0; i<s.ncourses; i++)
{
printf("%d) %s\t",i+1, s.courses_codes[i]);
}
printf("\n\n");
return;
}
void print_instructor(struct instructor ins)
{
printf("ID : %s\t\t\t", ins.id);
printf("Name : %s\t", ins.name);
printf("\nList of courses:\n");
for (int i=0; i<ins.ncourses; i++)
{
printf("%d) %s\t",i+1, ins.courses_codes[i]);
}
printf("\n\n");
return;
}
void print_course(struct course c)
{
printf("Course code.: %s\t\t", c.code);
printf("Name: %s", c.name);
printf("\nCredits: %d\n", c.credits);
printf("List of instructors: \n");
for (int i=0; i<c.ninstructors; i++)
{
printf("%d) %s\t",i+1, c.instructor_ids[i]);
}
printf("\nList of students:\n");
for (int i=0; i<c.nstudents; i++)
{
printf("%d) %s\t",i+1, c.student_rollnos[i]);
}
printf("\n\n");
return;
}
struct student modify_student(struct student s)
{ char str[MAX];
printf("Enter the detail You wish to change/modify : ");
printf("\nie name OR branch OR semester OR courses : ");
scanf("%s",str);
if(strcmp(str,"name")==0)
{ printf("Enter New Name :");
scanf("%s",s.name);
}
else if (strcmp(str,"branch")==0)
{ printf("Enter New Branch 1)CSE 2)EE 3)ME :");
scanf("%u",&s.branch);
}
else if (strcmp(str,"semester")==0)
{printf("Enter New Semester :");
scanf("%d", &s.semester);
}
else if (strcmp(str,"courses")==0)
{printf("Enter Changed No of courses :");
scanf("%d", &s.ncourses);
s.courses_codes = malloc (s.ncourses * sizeof(char *));
for (int j=0; j<s.ncourses; j++)
{
s.courses_codes[j] = malloc (MAX * sizeof (char));
printf("Enter course code %d",j+1," :");
scanf("%s", s.courses_codes[j]);
}
}
return(s);
}
struct instructor modify_instructor(struct instructor ins )
{ char str[MAX];
printf("Enter the detail to changed/modified : ");
printf("\nie name OR courses : ");
scanf("%s",str);
if(strcmp(str,"name")==0)
{ printf("Enter New Name :");
scanf("%s",ins.name);
}
else if (strcmp(str,"courses")==0)
{printf("Enter new number of courses");
scanf("%d",&ins.ncourses);
ins.courses_codes = malloc (ins.ncourses * sizeof(char *));
printf("Enter the courses :\n");
for (int i=0; i<ins.ncourses; i++)
{ printf("Enter course %d) code",i+1);
ins.courses_codes[i] = malloc (MAX * sizeof (char));
scanf("%s", ins.courses_codes[i]);
}
}
return(ins);
}
struct course modify_course(struct course c)
{ char str[MAX];
printf("Enter the detail to be changed/modified");
printf("\nie name OR credits OR instructors OR students : ");
scanf("%s",str);
if(strcmp(str,"name")==0)
{printf("Enter New Name of course : ");
scanf("%s",c.name);
}
else if(strcmp(str,"credits")==0)
{printf("Enter total Credits of course : ");
scanf("%d",&c.credits);
}
else if(strcmp(str,"instructors")==0)
{printf("Enter new number of instructors for that course :");
scanf("%d",&c.ninstructors);
c.instructor_ids = malloc(c.ninstructors * sizeof(char *));
printf("Enter the id of the instructors :\n");
for (int i=0; i<c.ninstructors; i++)
{printf("Enter instructor %d) ID :",i+1);
c.instructor_ids[i] = malloc (MAX * sizeof (char));
scanf("%s", c.instructor_ids[i]);
}
}
else if (strcmp(str,"students")==0)
{printf("Enter of new number students in course :");
scanf("%d",&c.nstudents);
c.student_rollnos = malloc(c.nstudents * sizeof(char *));
printf("Enter the roll no of students\n");
for (int i=0; i<c.nstudents; i++)
{printf("Enter student %d) rollno :",i+1);
c.student_rollnos[i] = malloc (MAX * sizeof (char));
scanf("%s", c.student_rollnos[i]);
}
} return(c);
}
void load_records()
{
load_students();
load_instructors();
load_courses();
}
void write_records()
{
write_students();
write_instructors();
write_courses();
}
void load_students()
{
fps = fopen("students.txt", "r");
int nstudents;
char ch;
char buff[MAX];
struct student s;
fscanf(fps, "%d", &nstudents);
printf("Total students in Record :%d\n",nstudents);
for (int i=0; i<nstudents; i++)
{
fscanf(fps, "%s", s.rollno );
fscanf(fps, "%s", s.name );
label :
ch=fgetc(fps);
ch=fgetc(fps);
if(ch<'0'||ch>'9')
{ fseek(fps, -2, SEEK_CUR );
fscanf(fps, "%s" ,buff);
strcat(s.name,"_");
strcat(s.name,buff);
goto label ;
}
else if(ch>='0'||ch<='9')
fseek( fps, -2, SEEK_CUR );
fscanf(fps, "%u", &s.branch);
fscanf(fps, "%d", &s.semester);
fscanf(fps, "%d", &s.ncourses);
s.courses_codes = malloc (s.ncourses * sizeof(char *));
for (int j=0; j<s.ncourses; j++)
{
s.courses_codes[j] = malloc (MAX * sizeof (char));
fscanf(fps, "%s", s.courses_codes[j] );
ch=fgetc(fps);
}ch=fgetc(fps);
add_student(s);
} fclose(fps);
return;
}
void load_instructors()
{ char ch;
fpi = fopen("instructors.txt","r");
int ninstructors;
char buff[MAX];
struct instructor ins;
fscanf(fpi,"%d",&ninstructors);
printf("Total instructors in Record :%d\n",ninstructors);
for (int i=0; i<ninstructors; i++)
{
ch=fgetc(fpi);
fscanf(fpi, "%s", ins.id);
fscanf(fpi, "%s", ins.name);
label :
ch=fgetc(fpi);
ch=fgetc(fpi);
if(ch<'0'||ch>'9')
{ fseek(fpi, -2, SEEK_CUR );
fscanf(fpi, "%s" ,buff);
strcat(ins.name,"_");
strcat(ins.name,buff);
goto label ;
}
else if(ch>='0'||ch<='9')
fseek( fpi, -2, SEEK_CUR );
fscanf(fpi, "%d", &ins.ncourses);
ins.courses_codes = malloc (ins.ncourses * sizeof(char *));
for (int j=0; j<ins.ncourses; j++)
{
ins.courses_codes[j] = malloc (MAX * sizeof (char));
fscanf(fpi, "%s", ins.courses_codes[j]);
ch=fgetc(fpi);
}
add_instructor(ins);
} fclose(fpi);
}
void load_courses()
{
fpc = fopen("courses.txt","r");
int ncourses;
char ch;
char buff[MAX];
fscanf(fpc,"%d",&ncourses);
printf("Total courses in Record :%d\n",ncourses);
for(int i=0;i<ncourses;i++)
{
ch=fgetc(fpc);
struct course c;
fscanf(fpc, "%s" ,c.code);
fscanf(fpc, "%s" ,c.name);
label :
ch=fgetc(fpc);
ch=fgetc(fpc);
if(ch<'0'||ch>'9')
{ fseek(fpc, -2, SEEK_CUR );
fscanf(fpc, "%s" ,buff);
strcat(c.name,"_");
strcat(c.name,buff);
goto label ;
}
else if(ch>='0'||ch<='9')
fseek( fpc, -2, SEEK_CUR );
fscanf(fpc, "%d" ,&c.credits);
fscanf(fpc, "%d" ,&c.ninstructors);
c.instructor_ids = malloc(c.ninstructors * sizeof(char *));
for (int j=0; j<c.ninstructors; j++)
{
c.instructor_ids[j] = malloc (MAX * sizeof (char));
fscanf(fpc, "%s", c.instructor_ids[j]);
ch=fgetc(fpc);
}
fscanf(fpc, "%d" ,&c.nstudents);
c.student_rollnos = malloc(c.nstudents * sizeof(char *));
for (int j=0; j<c.nstudents; j++)
{
c.student_rollnos[j] = malloc (MAX * sizeof (char));
fscanf(fpc, "%s", c.student_rollnos[j]);
ch=fgetc(fpc);
}
add_course(c);
} fclose(fpc);
}
void write_students()
{ fps = fopen("students.txt", "w");
fprintf(fps,"%d\n\n\n",nstudents);
for (int i=0; i<nstudents; i++)
{
fprintf(fps, "%s\n", students[i].rollno);
fprintf(fps, "%s\n", students[i].name);
fprintf(fps, "%u\n", students[i].branch);
fprintf(fps, "%d\n", students[i].semester);
fprintf(fps, "%d\n", students[i].ncourses);
for (int j=0; j<students[i].ncourses; j++)
{
fputs(students[i].courses_codes[j] , fps);
fputc('\t',fps);
}
fputc('\n',fps);
fputc('\n',fps);
}
fclose(fps);
}
void write_instructors()
{ fpi = fopen("instructors.txt","w");
fprintf(fpi,"%d\n\n\n",ninstructors);
for (int i=0; i<ninstructors; i++)
{
fprintf(fpi, "%s\n", instructors[i].id);
fprintf(fpi, "%s\n", instructors[i].name);
fprintf(fpi, "%d\n", instructors[i].ncourses);
for (int j=0; j<instructors[i].ncourses; j++)
fprintf(fpi, "%s\t", instructors[i].courses_codes[j]);
fputc('\n',fpi);
fputc('\n',fpi);
}
fclose(fpi);
}
void write_courses()
{
fpc = fopen("courses.txt","w");
fprintf(fpc,"%d\n\n\n",ncourses);
for(int i=0;i<ncourses;i++)
{ fprintf(fpc,"%s\n",courses[i].code);
fprintf(fpc,"%s\n",courses[i].name);
fprintf(fpc,"%d\n",courses[i].credits);
fprintf(fpc, "%d\n" ,courses[i].ninstructors);
for (int j=0; j<courses[i].ninstructors; j++)
fprintf(fpc, "%s\t", courses[i].instructor_ids[j]);
fputc('\n', fpc);
fprintf(fpc, "%d\n" ,courses[i].nstudents);
for (int j=0; j<courses[i].nstudents; j++)
fprintf(fpc, "%s\t", courses[i].student_rollnos[j]);
fputc('\n',fpc);
fputc('\n',fpc);
}
fclose(fpc);
}