forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent-Record-Management.c
More file actions
140 lines (131 loc) · 3.6 KB
/
Student-Record-Management.c
File metadata and controls
140 lines (131 loc) · 3.6 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int id;
char name[50];
int age;
float marks;
};
void addStudent(FILE *fp) {
struct Student s;
printf("\nEnter ID: ");
scanf("%d", &s.id);
printf("Enter Name: ");
getchar();
fgets(s.name, 50, stdin);
s.name[strcspn(s.name, "\n")] = 0;
printf("Enter Age: ");
scanf("%d", &s.age);
printf("Enter Marks: ");
scanf("%f", &s.marks);
fwrite(&s, sizeof(s), 1, fp);
printf("\n✅ Student record added successfully!\n");
}
void displayStudents(FILE *fp) {
struct Student s;
rewind(fp);
printf("\n---- Student Records ----\n");
while (fread(&s, sizeof(s), 1, fp) == 1) {
printf("ID: %d\nName: %s\nAge: %d\nMarks: %.2f\n----------------\n", s.id, s.name, s.age, s.marks);
}
}
void searchStudent(FILE *fp) {
struct Student s;
int id, found = 0;
printf("\nEnter ID to search: ");
scanf("%d", &id);
rewind(fp);
while (fread(&s, sizeof(s), 1, fp) == 1) {
if (s.id == id) {
printf("\n🎯 Student Found:\nID: %d\nName: %s\nAge: %d\nMarks: %.2f\n", s.id, s.name, s.age, s.marks);
found = 1;
break;
}
}
if (!found)
printf("\n❌ Student with ID %d not found!\n", id);
}
void updateStudent(FILE *fp) {
struct Student s;
int id, found = 0;
long pos;
printf("\nEnter ID to update: ");
scanf("%d", &id);
rewind(fp);
while (fread(&s, sizeof(s), 1, fp) == 1) {
if (s.id == id) {
found = 1;
pos = ftell(fp) - sizeof(s);
printf("\nEnter new Name: ");
getchar();
fgets(s.name, 50, stdin);
s.name[strcspn(s.name, "\n")] = 0;
printf("Enter new Age: ");
scanf("%d", &s.age);
printf("Enter new Marks: ");
scanf("%f", &s.marks);
fseek(fp, pos, SEEK_SET);
fwrite(&s, sizeof(s), 1, fp);
printf("\n✅ Record updated successfully!\n");
break;
}
}
if (!found)
printf("\n❌ Student with ID %d not found!\n", id);
}
void deleteAll(FILE *fp) {
fclose(fp);
fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("\nError deleting records.\n");
return;
}
printf("\n🗑️ All student records deleted successfully!\n");
fclose(fp);
}
int main() {
FILE *fp;
int choice;
fp = fopen("students.dat", "ab+");
if (fp == NULL) {
printf("Error opening file!\n");
exit(1);
}
while (1) {
printf("\n===== STUDENT RECORD SYSTEM =====");
printf("\n1. Add Student");
printf("\n2. Display All Students");
printf("\n3. Search Student");
printf("\n4. Update Student");
printf("\n5. Delete All Records");
printf("\n6. Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(fp);
break;
case 2:
displayStudents(fp);
break;
case 3:
searchStudent(fp);
break;
case 4:
updateStudent(fp);
break;
case 5:
deleteAll(fp);
fp = fopen("students.dat", "ab+");
break;
case 6:
fclose(fp);
printf("\n👋 Exiting program...\n");
exit(0);
default:
printf("\n❌ Invalid choice! Try again.\n");
}
}
return 0;
}