-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.c
More file actions
281 lines (258 loc) · 6.88 KB
/
Student.c
File metadata and controls
281 lines (258 loc) · 6.88 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
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
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void insert_record();
void display_record();
void search_record();
void del_record();
void update_record();
void sort_record();
struct student
{
int roll;
char sec[10];
char name[40];
int marks;
float grade;
};
struct student s;
void main()
{
int ch;
while(ch!=7)
{
printf("\n***********************************************\n");
printf("\n WELCOME TO STUDENT MANAGEMENT PROGRAM\n");
printf("\n***********************************************\n\n\n");
printf("\t**** AVAILABLE FUNCTIONALITIES ****\n\n");
printf("\t1: Insert student record\n");
printf("\t2: Display student record\n");
printf("\t3: Search student record\n");
printf("\t4: Delete student record\n");
printf("\t5: Update student record\n");
printf("\t6: Sort student record\n");
printf("\t7: Exit\n\n");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_record();
break;
case 2:
display_record();
break;
case 3:
search_record();
break;
case 4:
del_record();
break;
case 5:
update_record();
break;
case 6:
sort_record();
break;
case 7:
exit(1);
default:
printf("\nWrong choice Entered");
}
printf("\nPress any key to continue ");
getch();
}
}
void insert_record()
{
FILE *fp;
fp=fopen("stu.txt","ab+");
if(fp==NULL)
{
printf("\nError: Cannot Open the File!!!");
return;
}
printf("\n **** Previous Stored Data ****");
display_record();
printf("\n**** ENTER NEW STUDENT DATA ****\n");
printf("\nEnter Student Roll Number: ");
scanf("%d",&s.roll);
fflush(stdin);
printf("\nEnter Student Name: ");
gets(s.name);
printf("\nEnter Student Section: ");
gets(s.sec);
printf("\nEnter Student Total marks: ");
scanf("%d",&s.marks);
printf("\nEnter Student Grade: ");
scanf("%f",&s.grade);
fwrite(&s,sizeof(s),1,fp);
{
printf("\n\n !!! Student Record Inserted Sucessfully\n");
}
fclose(fp);
printf("\n Updated Record !!\n");
display_record();
}
void display_record()
{
FILE *fp;
fp=fopen("stu.txt","rb");
if(fp==NULL)
{
printf("\nError : Cannot open the File !!!");
return;
}
printf("\n\n **** Students Details Are As Follows ****\n");
printf("Roll.No\t\tName of Student\t\tSection\t\tMarks\t\tGrade\n\n");
//printf("a.students roll number\t b.student name\t c.student section\t d.student marks\t e.student grade");
while(fread(&s,sizeof(s),1,fp)==1)
{
printf("%d\t%s\t%s \t\t%d\t\t%f\n",s.roll,s.name,s.sec,s.marks,s.grade);
}
fclose(fp);
}
void search_record()
{
int ro,flag=0;
FILE *fp;
fp=fopen("stu.txt","rb");
if(fp==NULL)
{
printf("\nError: Cannot Open the File!!!");
return;
}
printf("\n\nEnter Student Roll Number Which You Want To Search: ");
scanf("%d",&ro);
while(fread(&s,sizeof(s),1,fp)>0 && flag==0)
{
if(s.roll==ro)
{
flag=1;
printf("\n\nSearch Sucessfull And Student Records Is as Follows: \n\n");
printf("Roll.No\tName of Student\t\tSection\tMarks\tGrade\n\n");
printf("%d\t%s\t%s \t%d\t%f\t",s.roll,s.name,s.sec,s.marks,s.grade);
}
}
if(flag==0)
{
printf("\n\nNo Such Record Found !!!!!\n");
}
fclose(fp);
}
void del_record()
{
int ro,flag=0;
FILE *fp,*ft;
fp=fopen("stu.txt","rb");
ft=fopen("stu1.txt","ab+");
if(fp==NULL)
{
printf("\nError: Cannot Open the File!!!");
return;
}
printf("\n **** Previous Stored Data ****");
display_record();
printf("\n\nEnter Student Roll Number Which You Want to Delete ");
scanf("%d",&ro);
while(fread(&s,sizeof(s),1,fp)>0 && flag==0)
{
if(s.roll==ro)
{
flag=1;
printf("\nRecord Deleted Sucessfully \n");
}
else
{
fwrite(&s,sizeof(s),1,ft);
}
}
if(flag==0)
{
printf("\n\nNo Such Record Found !!!");
}
fclose(fp);
fclose(ft);
remove("stu.txt");
rename("stu1.txt","stu.txt");
printf("\n Updated Record !!\n");
display_record();
}
void update_record()
{
int ro,flag=0;
FILE *fp;
fp=fopen("stu.txt","rb+");
if(fp==NULL)
{
printf("\nError: Cannot Open the File!!!");
return;
}
printf("\n\nEnter Roll Number of Student Whose Record You Want To Update: ");
scanf("%d",&ro);
printf("\n*** Previously Stored Record Of Given Roll Number ***");
while(fread(&s,sizeof(s),1,fp)>0 && flag==0)
{
if(s.roll==ro)
{
flag=1;
printf("\tRoll.No\tName of Student\tSection\tMarks\tGrade\n\n");
printf("%d\t%s\t%s \t%d\t%f\t",s.roll,s.name,s.sec,s.marks,s.grade);
printf("\n*** Now Enter the New Record ***\n\n");
printf("\n\nUpdated Student Roll Number: ");
scanf("%d",&s.roll);
fflush(stdin);
printf("\nUpdated Student Name: ");
gets(s.name);
printf("\nUpdated Student Section: ");
gets(s.sec);
printf("\nUpdated Student Marks: ");
scanf("%d",&s.marks);
printf("\nUpdated Student Grade: ");
scanf("%f",&s.grade);
fseek(fp,-(long)sizeof(s),1);
fwrite(&s,sizeof(s),1,fp);
printf("\n\nRecord Updated Successfully Check The Display Section!!\n");
}
}
if(flag==0)
{
printf("\n\nError : Something went wrong!!!");
}
fclose(fp);
}
void sort_record()
{
struct student temp;
struct student arr[50];
int i,j,k=0;
FILE *fp;
fp=fopen("stu.txt","rb");
if(fp==NULL)
{
printf("\n\nError: Cannot Open File !!!\n");
}
i=0;
while(fread(&arr[i],sizeof(arr[i]),1,fp)==1)
{
i++;k++;
}
for(i=0;i<k;i++)
{
for(j=0;j<k-i-1;j++)
{
if(arr[j].roll>arr[j+1].roll)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\nAfter Sorting Student Details !!\n\n");
for(i=0;i<k;i++)
{
printf("%d\t\t%s\t\t%s\t%d\t%f\n",arr[i].roll,arr[i].name,arr[i].sec,arr[i].marks,arr[i].grade);
}
fclose(fp);
}