-
Notifications
You must be signed in to change notification settings - Fork 0
/
caevaluator.c
432 lines (375 loc) · 13.4 KB
/
caevaluator.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
#include<stdio.h>
#include <string.h>
struct subject {
int subjectcode;
char subjectname[30];
int subjectmark;
};
struct student {
int rollno;
char name[50];
char section[10];
struct subject s;
};
struct student stu;
void create_record();
void search_record();
void modify_record();
void delete_record();
void display_record();
int main()
{
int choice;
while(1){
printf("\nMenu:\n");
printf("1. Create Student Record\n");
printf("2. Search Student Record\n");
printf("3. Modify Student Record\n");
printf("4. Delete Student Record\n");
printf("5. Display Student Record\n");
printf("0. EXIT!!\n\n");
printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
create_record();
break;
case 2:
search_record();
break;
case 3:
modify_record();
break;
case 4:
delete_record();
break;
case 5:
display_record();
break;
case 0:
printf("\nProgram is closing. Thank you for using the Student Record Management System!\n");
break;
default:
printf("\nInvalid input. Please try again.\n");
break;
}
if(choice==0)
{
break;
}
}
return 0;
}
void create_record()
{
FILE *fp;
fp = fopen("student_records.txt", "ab+");
if (fp == NULL)
{
printf("\n\t\tError: Unable to open the specified file. Please check if the file exists and you have sufficient permissions to access it.\n");
return;
}
int num_records;
printf("\n\n\t\t** Enter new student data **\n\n");
printf("\n\t\tEnter the number of student records to upload: ");
scanf("%d", &num_records);
fflush(stdin);
for (int i = 0; i < num_records; i++)
{
printf("\n\t\tEnter Student Roll Number: ");
scanf("%d", &stu.rollno);
fflush(stdin);
printf("\n\t\tEnter Student Name: ");
gets(stu.name);
printf("\n\t\tEnter Student Section: ");
gets(stu.section);
printf("\n\t\tEnter Subject Code: ");
scanf("%d", &stu.s.subjectcode);
printf("\n\t\tEnter subject Name: ");
scanf("%s", &stu.s.subjectname);
printf("\n\t\tEnter Subject Mark: ");
scanf("%d", &stu.s.subjectmark);
fwrite(&stu, sizeof(stu), 1, fp);
printf("\n\t\t--- Student Record %d Inserted Successfully ---\n", i + 1);
}
fclose(fp);
printf("\n\t\tUpdated Record \n");
display_record();
}
void search_record()
{
int ro, flag = 0;
char choice;
FILE *fp;
fp = fopen("student_records.txt", "rb");
if (fp == NULL)
{
printf("\n\t\tError: Unable to open the specified file. Please check if the file exists and you have sufficient permissions to access it.\n"); // Print error message if file cannot be opened
return;
}
printf("\n\n\tPlease choose the search category:\n");
printf("\t\t1. Roll number\n");
printf("\t\t2. Name\n");
printf("\t\t3. Subject code\n");
printf("\tYour choice: ");
scanf(" %c", &choice);
switch (choice)
{
case '1':
printf("\n\n\tPlease enter the roll number of the student you would like to search for: ");
scanf("%d", &ro);
while (fread(&stu, sizeof(stu), 1, fp) > 0 && flag == 0)
{
if (stu.rollno == ro)
{
flag = 1;
printf("\n\n\tSearch Successfully And Student Records is as follows:\n\n");
printf("\nRoll.No\t\tName of Student\t\tSection\t\tSubject Code\t\tSubject Name\t\tMark\n\n");
printf("%-15d %-10s\t\t%s\t\t%-10d\t\t%-10s\t\t%d\n", stu.rollno, stu.name, stu.section, stu.s.subjectcode, stu.s.subjectname, stu.s.subjectmark); // Display record
}
}
if (flag == 0)
{
printf("\n\n\tNo Search Record Found\n");
}
break;
case '2':
printf("\n\n\tPlease enter the name of the student you would like to search for: ");
char name[50];
scanf("%s", name);
while (fread(&stu, sizeof(stu), 1, fp) > 0 && flag == 0)
{
if (strcmp(stu.name, name) == 0)
{
flag = 1;
printf("\n\n\tSearch Successfully And Student Records is as follows:\n\n");
printf("\nRoll.No\t\tName of Student\t\tSection\t\tSubject Code\t\tSubject Name\t\tMark\n\n");
printf("%-15d %-10s\t\t%s\t\t%-10d\t\t%-10s\t\t%d\n", stu.rollno, stu.name, stu.section, stu.s.subjectcode, stu.s.subjectname, stu.s.subjectmark); // Display record
}
}
if (flag == 0)
{
printf("\n\n\tNo Search Record Found\n");
}
break;
case '3':
printf("\n\n\tPlease enter the subject code of the student you would like to search for: ");
int subjectcode;
scanf("%d", &subjectcode);
while (fread(&stu, sizeof(stu), 1, fp) > 0 && flag == 0)
{
if (stu.s.subjectcode == subjectcode)
{
flag = 1;
printf("\n\n\tSearch Successfully And Student Records is as follows:\n\n");
printf("\nRoll.No\t\tName of Student\t\tSection\t\tSubject Code\t\tSubject Name\t\tMark\n\n");
printf("%-15d %-10s\t\t%s\t\t%-10d\t\t%-10s\t\t%d\n", stu.rollno, stu.name, stu.section, stu.s.subjectcode, stu.s.subjectname, stu.s.subjectmark); // Display record
}
}
if (flag == 0)
{
printf("\n\n\tNo Search Record Found\n");
}
break;
default:
printf("\n\n\tInvalid choice\n");
break;
}
fclose(fp);
}
void modify_record()
{
int ro, flag = 0, choice;
FILE *fp;
fp = fopen("student_records.txt", "rb+");
if (fp == NULL)
{
printf("\n\t\tError: Unable to open the specified file. Please check if the file exists and you have sufficient permissions to access it.\n"); // Print error message if file cannot be opened
return;
}
printf("\n\n\tPlease enter the roll number of the student whose record you want to update: ");
scanf("%d", &ro);
printf("\n\t\t-----Previously stored record for the given roll number-----");
while (fread(&stu, sizeof(stu), 1, fp) > 0)
{
if (stu.rollno == ro)
{
flag = 1; // Set flag to indicate record found
printf("\nRoll.No\t\tName of Student\t\tSection\t\tSubject Code\t\tSubject Name\t\tMark\n\n");
printf("%-15d %-10s\t\t%s\t\t%-10d\t\t%-10s\t\t%d\n", stu.rollno, stu.name, stu.section, stu.s.subjectcode, stu.s.subjectname, stu.s.subjectmark);
printf("\n\t\t-----Now Enter The New Record-----\n\n");
printf("\n\t\tWhich field do you want to update?");
printf("\n\t\t1. Student Roll Number");
printf("\n\t\t2. Student Name");
printf("\n\t\t3. Student Section");
printf("\n\t\t4. Subject Code");
printf("\n\t\t5. Subject Name");
printf("\n\t\t6. Subject Mark\n");
printf("\n\t\tEnter your choice (1-6): ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("\n\t\tPlease enter the updated student roll number: ");
scanf("%d", &stu.rollno);
break;
case 2:
fflush(stdin);
printf("\n\t\tPlease enter the updated student name: ");
gets(stu.name);
break;
case 3:
fflush(stdin);
printf("\n\t\tPlease enter the updated student section: ");
gets(stu.section);
break;
case 4:
printf("\n\t\tPlease enter the updated subject code: ");
scanf("%d", &stu.s.subjectcode);
break;
case 5:
printf("\n\t\tPlease enter the updated subject name: ");
scanf("%s", stu.s.subjectname);
break;
case 6:
printf("\n\t\tPlease enter the updated subject mark: ");
scanf("%d", &stu.s.subjectmark);
break;
default:
printf("\n\t\tInvalid choice. Please try again.\n");
break;
}
fseek(fp, -(long)sizeof(stu), SEEK_CUR);
fwrite(&stu, sizeof(stu), 1, fp);
printf("\n\t\tRecord updated successfully!\n");
break;
}
}
if (flag == 0)
{
printf("\n\t\tError: Record not found. Please check if the roll number entered is correct.\n");
}
fclose(fp);
}
void delete_record()
{
char name[40];
unsigned flag=0;
int choice;
FILE *fp,*ft;
fp=fopen("student_records.txt","rb");
if(fp==NULL)
{
printf("\n\t\tError: Unable to open the specified file. Please check if the file exists and you have sufficient permissions to access it.\n");
return;
}
printf("\n\t -----Previous Stored Data -----");
display_record();
printf("\n\n\t1. Delete a single record");
printf("\n\t2. Delete multiple records");
printf("\n\tEnter your choice (1 or 2): ");
scanf("%d",&choice);
if(choice==1)
{
printf("\n\tEnter the name of the student you want to delete: ");
scanf("%s",name);
ft=fopen("student_records1.txt","ab+");
while(fread(&stu,sizeof(stu),1,fp)==1)
{
if(strcmp(name,stu.name)!=0)
{
fwrite(&stu,sizeof(stu),1,ft);
}
else
{
flag=1;
printf("\n\t\tRecord has been deleted successfully.\n");
}
}
if(flag==0)
{
printf("\n\n\t\tNo Such Record Found\n");
}
fclose(fp);
fclose(ft);
remove("student_records.txt");
rename("student_records1.txt","student_records.txt");
}
else if(choice==2)
{
int count=0;
printf("\n\tEnter the names of the students you want to delete one by one (type \"end\" to stop): \n");
ft=fopen("student_records1.txt","ab+");
while(1)
{
printf("\n\tName: ");
scanf("%s",name);
if(strcmp(name,"end")==0)
{
break;
}
flag=0;
rewind(fp);
while(fread(&stu,sizeof(stu),1,fp)==1)
{
if(strcmp(name,stu.name)==0)
{
flag=1;
count++; e
printf("\n\t\tRecord has been deleted successfully.\n");
break;
}
}
if(flag==0)
{
printf("\n\n\t\tNo Such Record Found\n");
}
}
fclose(fp);
rewind(fp);
while(fread(&stu,sizeof(stu),1,fp)==1)
{
flag=0;
for(int i=0;i<count;i++)
{
if(strcmp(name,stu.name)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
fwrite(&stu,sizeof(stu),1,ft);
}
}
fclose(fp);
fclose(ft);
remove("student_records.txt");
rename("student_records1.txt","student_records.txt");
printf("\n\t\t%d records have been deleted successfully.\n",count);
}
else
{
printf("\n\tInvalid Choice. Please Try Again.\n");
}
}
void display_record()
{
FILE *fp;
fp = fopen("student_records.txt", "rb");
if (fp == NULL)
{
printf("\n\t\tError: Unable to open the specified file. Please check if the file exists and you have sufficient permissions to access it.\n");
return;
}
printf("\n\n\t+-----------------+----------------------+---------+--------------+----------------------+------+\n");
printf("\t| Roll.No | Name of Student | Section | Subject Code | Subject Name | Mark |\n");
printf("\t+-----------------+----------------------+---------+--------------+----------------------+------+\n");
while (fread(&stu, sizeof(stu), 1, fp) == 1)
{
printf("\t| %-15d | %-20s | %-7s | %-12d | %-20s | %-4d |\n", stu.rollno, stu.name, stu.section, stu.s.subjectcode, stu.s.subjectname, stu.s.subjectmark);
}
printf("\t+-----------------+----------------------+---------+--------------+----------------------+------+\n");
fclose(fp);
}