-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilehandler.c
More file actions
143 lines (117 loc) Β· 3.87 KB
/
filehandler.c
File metadata and controls
143 lines (117 loc) Β· 3.87 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
#include <stdio.h>
#include <string.h>
#include "student.c"
void addRecord();
void updateRecord();
void viewRecords();
void addRecord() {
Student s;
printf("\n----- Add New Student Record -----\n");
printf("Enter Student ID: ");
scanf("%d", &s.id);
printf("Enter Name: ");
getchar();
fgets(s.name, 50, stdin);
s.name[strcspn(s.name, "\n")] = 0;
printf("Enter Roll No: ");
scanf("%d", &s.roll);
printf("Enter marks for 5 subjects:\n");
printf("Math: ");
scanf("%f", &s.tma);
printf("Physics: ");
scanf("%f", &s.tph);
printf("English: ");
scanf("%f", &s.thu);
printf("C Programming: ");
scanf("%f", &s.tcs);
printf("Electrical: ");
scanf("%f", &s.tee);
s = calculateGrade(s);
FILE *fp = fopen("Records.txt", "a");
if (fp != NULL) {
fprintf(fp, "%d %s %d %.2f %.2f %.2f %.2f %.2f %d %.2f %c\n",
s.id, s.name, s.roll, s.tma, s.tph, s.thu, s.tcs, s.tee,
s.total, s.percentage, s.grade);
fclose(fp);
printf("\nRecord added successfully!\n");
displayDetail(s);
} else {
printf("Error opening file!\n");
}
}
void updateRecord() {
int searchRoll;
printf("\nEnter Roll Number to update: ");
scanf("%d", &searchRoll);
FILE *fp = fopen("Records.txt", "r");
FILE *temp = fopen("temp.txt", "w");
if (fp == NULL || temp == NULL) {
printf("Error opening file!\n");
return;
}
Student s;
int found = 0;
char line[256];
while (fgets(line, sizeof(line), fp)) {
sscanf(line, "%d %[^0-9] %d %f %f %f %f %f %d %f %c",
&s.id, s.name, &s.roll, &s.tma, &s.tph, &s.thu,
&s.tcs, &s.tee, &s.total, &s.percentage, &s.grade);
if (s.roll == searchRoll) {
found = 1;
printf("\nCurrent Record:\n");
displayDetail(s);
printf("\nEnter new details:\n");
printf("Enter new name: ");
getchar();
fgets(s.name, 50, stdin);
s.name[strcspn(s.name, "\n")] = 0;
printf("Enter new marks:\n");
printf("Math: ");
scanf("%f", &s.tma);
printf("Physics: ");
scanf("%f", &s.tph);
printf("English: ");
scanf("%f", &s.thu);
printf("C Programming: ");
scanf("%f", &s.tcs);
printf("Electrical: ");
scanf("%f", &s.tee);
s = calculateGrade(s);
printf("\nUpdated Record:\n");
displayDetail(s);
}
fprintf(temp, "%d %s %d %.2f %.2f %.2f %.2f %.2f %d %.2f %c\n",
s.id, s.name, s.roll, s.tma, s.tph, s.thu, s.tcs, s.tee,
s.total, s.percentage, s.grade);
}
fclose(fp);
fclose(temp);
if (found) {
remove("Records.txt");
rename("temp.txt", "Records.txt");
printf("Record updated successfully!\n");
} else {
remove("temp.txt");
printf("Roll number %d not found!\n", searchRoll);
}
}
void viewRecords() {
FILE *fp = fopen("Records.txt", "r");
if (fp == NULL) {
printf("No records found!\n");
return;
}
printf("\n----- All Student Records -----\n");
Student s;
char line[256];
int count = 0;
while (fgets(line, sizeof(line), fp)) {
sscanf(line, "%d %[^0-9] %d %f %f %f %f %f %d %f %c",
&s.id, s.name, &s.roll, &s.tma, &s.tph, &s.thu,
&s.tcs, &s.tee, &s.total, &s.percentage, &s.grade);
displayDetail(s);
count++;
}
fclose(fp);
printf("\nTotal records: %d\n", count);
}