-
Notifications
You must be signed in to change notification settings - Fork 0
/
studentRegistrationSystem.c
199 lines (175 loc) · 4.85 KB
/
studentRegistrationSystem.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct student{
char ID[6];
char name[20];
char add[20];
char fac[20];
char depat[100];
int phone_no;
};
struct student stu;
void add_student(){
printf("Add Record \n");
FILE *fp;
fp = fopen("record.txt","ab+");
if(fp == NULL){
printf("Error in Opening file\nMake sure your file is not write protected","Warning");
}else{
fflush(stdin);
printf("ID: ");gets(stu.ID);
if(strlen(stu.ID)>=6){
printf("wrong id format !!! \n\n");
}
else{
printf("Name: ");gets(stu.name);
printf("Address: ");gets(stu.add);
printf("Faculty: ");gets(stu.fac);
printf("Department: ");gets(stu.depat);
printf("Phone Number: ");scanf("%d",&stu.phone_no);
fwrite(&stu, sizeof(stu), 1, fp);
printf("The record is sucessfully added \n");
}
}
fclose(fp);
return;
}
void search_student(){
printf("Search Record \n");
char s_id[15];
int isFound = 0;
printf("Enter ID to Search: ");
fflush(stdin);
gets(s_id);
FILE *fp;
fp = fopen("record.txt","rb");
while(fread(&stu,sizeof(stu),1,fp) == 1){
if(strcmp(s_id,stu.ID) == 0){
isFound = 1;
break;
}
}
if(isFound == 1){
printf("\n************************************\n");
printf("The record is Found! *\n");
printf("************************************\n");
printf("*ID: %s \n",stu.ID);
printf("*Name: %s \n",stu.name);
printf("*Address: %s \n",stu.add);
printf("*Faculty: %s \n",stu.fac);
printf("*Department: %s \n",stu.depat);
printf("*Phone No: %d \n",stu.phone_no);
printf("************************************\n\n\n");
}
else{
printf("Sory, No record found in the database \n");
}
fclose(fp);
return;
}
void mod_student(){
printf("Modify Record \n");
char s_id[15];
int isFound = 0;
printf("Enter ID to Modify: ");fflush(stdin);
gets(s_id);
FILE *fp;s
fp = fopen("record.txt","rb+");
while(fread(&stu, sizeof(stu),1,fp) == 1){
if(strcmp(s_id, stu.ID) == 0){
fflush(stdin);
printf("ID: ");gets(stu.ID);
printf("Name: ");gets(stu.name);
printf("Address: ");gets(stu.add);
printf("Faculty: ");gets(stu.fac);
printf("Department: ");gets(stu.depat);
printf("Phone Number: ");scanf("%d",&stu.phone_no);
fseek(fp,sizeof(stu), SEEK_CUR);
fwrite(&stu,sizeof(stu), 1, fp);
isFound = 1;
break;
}
}
if(!isFound){
printf("\n\n\n No Record Found \n\n\n");
}
fclose(fp);
return;
}
void delete_student(){
printf("Delete Record \n");
char s_id[15];
int isFound = 0;
printf("Enter ID to Modify: ");fflush(stdin);
gets(s_id);
FILE *fp, *temp;
fp = fopen("record.txt","rb");
temp = fopen("temp.txt", "wb");
while(fread(&stu, sizeof(stu),1,fp) == 1){
if(strcmp(s_id, stu.ID) == 0){
fwrite(&stu,sizeof(stu),1,temp);
}
}
fclose(fp);
fclose(temp);
remove("record.txt");
rename("temp.txt","record.txt");
printf("\n\nThe record is sucessfully deleted \n\n\n");
return;
}
void display_data(){
FILE *fp;
fp = fopen("record.txt","rb");
while(fread(&stu,sizeof(stu),1,fp) == 1){
printf("************************************\n");
printf("*ID: %s \n",stu.ID);
printf("*Name: %s \n",stu.name);
printf("*Address: %s \n",stu.add);
printf("*Faculty: %s \n",stu.fac);
printf("*Department: %s \n",stu.depat);
printf("*Phone No: %d \n",stu.phone_no);
printf("************************************\n\n\n");
}
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tEnd of file\n");
fclose (fp);
// return;
}
int main() {
char choice;
while(1){
printf("1. Add Student \n");
printf("2. Search Student \n");
printf("3. Modify Student Record \n");
printf("4. Delete Student Record \n");
printf("5. Display all records \n");
printf("6. Exit \n");
printf("Enter your choice: ");
scanf("%c",&choice);
switch(choice){
case '1':
add_student();
break;
case '2':
search_student();
break;
case '3':
mod_student();
break;
case '4':
delete_student();
break;
case '5':
display_data();
break;
case '6':
exit(0);
break;
default:
printf("\n wrong input please try again \n");
break;
}
}
return 0;
}