-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudent.c
More file actions
141 lines (132 loc) · 3.71 KB
/
student.c
File metadata and controls
141 lines (132 loc) · 3.71 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int id;
char name[50];
int age;
char grade[10];
};
void addStudent();
void displayStudents();
void searchStudent();
void deleteStudent();
void updateStudent();
int main() {
int choice;
while (1) {
printf("\n===== Student Management System =====\n");
printf("1. Add Student\n");
printf("2. Display Students\n");
printf("3. Search Student\n");
printf("4. Update Student\n");
printf("5. Delete Student\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: addStudent(); break;
case 2: displayStudents(); break;
case 3: searchStudent(); break;
case 4: updateStudent(); break;
case 5: deleteStudent(); break;
case 6: exit(0);
default: printf("Invalid choice! Try again.\n");
}
}
return 0;
}
void addStudent() {
FILE *fp = fopen("students.dat", "ab");
struct Student s;
printf("Enter ID: ");
scanf("%d", &s.id);
printf("Enter Name: ");
getchar(); // clear buffer
gets(s.name);
printf("Enter Age: ");
scanf("%d", &s.age);
printf("Enter Grade: ");
scanf("%s", s.grade);
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);
printf("Record added successfully!\n");
}
void displayStudents() {
FILE *fp = fopen("students.dat", "rb");
struct Student s;
if (!fp) {
printf("No records found!\n");
return;
}
printf("\nID\tName\t\tAge\tGrade\n");
printf("-------------------------------------\n");
while (fread(&s, sizeof(s), 1, fp))
printf("%d\t%-15s\t%d\t%s\n", s.id, s.name, s.age, s.grade);
fclose(fp);
}
void searchStudent() {
FILE *fp = fopen("students.dat", "rb");
struct Student s;
int id, found = 0;
printf("Enter ID to search: ");
scanf("%d", &id);
while (fread(&s, sizeof(s), 1, fp)) {
if (s.id == id) {
printf("Record found!\n");
printf("ID: %d\nName: %s\nAge: %d\nGrade: %s\n", s.id, s.name, s.age, s.grade);
found = 1;
break;
}
}
if (!found) printf("Record not found!\n");
fclose(fp);
}
void deleteStudent() {
FILE *fp = fopen("students.dat", "rb");
FILE *temp = fopen("temp.dat", "wb");
struct Student s;
int id, found = 0;
printf("Enter ID to delete: ");
scanf("%d", &id);
while (fread(&s, sizeof(s), 1, fp)) {
if (s.id != id)
fwrite(&s, sizeof(s), 1, temp);
else
found = 1;
}
fclose(fp);
fclose(temp);
remove("students.dat");
rename("temp.dat", "students.dat");
if (found)
printf("Record deleted successfully!\n");
else
printf("Record not found!\n");
}
void updateStudent() {
FILE *fp = fopen("students.dat", "rb+");
struct Student s;
int id, found = 0;
printf("Enter ID to update: ");
scanf("%d", &id);
while (fread(&s, sizeof(s), 1, fp)) {
if (s.id == id) {
printf("Enter new Name: ");
getchar();
gets(s.name);
printf("Enter new Age: ");
scanf("%d", &s.age);
printf("Enter new Grade: ");
scanf("%s", s.grade);
fseek(fp, -sizeof(s), SEEK_CUR);
fwrite(&s, sizeof(s), 1, fp);
found = 1;
printf("Record updated successfully!\n");
break;
}
}
if (!found)
printf("Record not found!\n");
fclose(fp);
}