-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdmng.c
More file actions
282 lines (259 loc) · 7.82 KB
/
stdmng.c
File metadata and controls
282 lines (259 loc) · 7.82 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
282
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// struture for the students data.
struct std
{
char name[10], lastname[20];
int id;
char dep[20];
char course[10];
};
// pause
void pause()
{
printf("\nPress Enter to Continue...");
while (getchar() != '\n') // clear any leftover input
;
getchar(); // wait for key
}
// add student function
void addStudent()
{
FILE *fp;
struct std s;
printf("\n----Add a Student----\n");
fp = fopen("student.txt", "a");
if (fp == NULL)
{
printf("\nERROR : File can'nt be opened.\n");
}
// fprintf(fp, "Roll No., Name, Department, Course\n");
// student input
printf("\nEnter Roll no. : ");
scanf("%d", &s.id); // the getchar is only needed after scanf function because,See the infoabout.txt.
printf("\nEnter Firstname : ");
// gets(s.name);
// scanf("%s", s.name);
getchar(); // without this the fgets will not be able to take any input due to the new line character.The new line character can here due to pressing of Enter after typing the roll no., that's why i used the getchar() function to eat up that \n character.
fgets(s.name, sizeof(s.name), stdin);
s.name[strcspn(s.name, "\n")] = '\0'; // to remove the newline character generated by the fgets function.
printf("\nEnter Lastname : ");
fgets(s.lastname, sizeof(s.lastname), stdin);
s.lastname[strcspn(s.lastname, "\n")] = '\0';
printf("\nDepartment : ");
// getchar();No need. check the infoabout.txt,about the fgets() fucntion work.
fgets(s.dep, sizeof(s.dep), stdin);
s.dep[strcspn(s.dep, "\n")] = '\0';
printf("\nCourse : ");
// getchar();
fgets(s.course, sizeof(s.course), stdin);
s.course[strcspn(s.course, "\n")] = '\0';
// write to File
fprintf(fp, "%d %s %s %s %s\n", s.id, s.name, s.lastname, s.dep, s.course);
fclose(fp);
printf("\nStudent Added Successfully.");
pause();
}
// View all students
void viewallstd()
{
printf("\n----All Students----\n");
FILE *fp = fopen("student.txt", "r");
char str[200];
// char str[sizeof("student.txt")];
// fgets(str, sizeof("student.txt"), fp); Wrong, size of "student.txt" only takes the size 12 length of the string(stduent.txt)not the file content.MEans it can only take upto 12 character long string.
if (fp == NULL)
{
printf("\nERROR : Cann't open the file.");
}
/*fgets(str, sizeof(str), fp); // here fgets is run once and hence the first line of the file gets called.if i run another fgets command the second line should be printed.
printf("%s", str);*/
// fgets(str, sizeof(str), fp);
// printf("%s", str); This prints the next line so. we have to loop it up untill the file ends i.e EOF(End Of File).
while (fgets(str, sizeof(str), fp) != NULL)
{
printf("%s", str);
}
fclose(fp);
pause();
}
// Search a student
void searchstd()
{
printf("\n----Search a Student----\n");
struct std s;
int n;
char line[40];
int found = 0;
printf("Enter Roll no. : ");
scanf("%d", &n);
FILE *fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("\nERROR : File not Found");
return;
}
while (fgets(line, sizeof(line), fp) != NULL)
{
// printf("\nRaw line: %s\n", line); // this will show what exactly is being read
// check for parsing : debug
// printf("\nParsed Roll: %d | Name: %s %s | Dept: %s | Course: %s\n", roll, name, lastname, dep, course);
int parsed = sscanf(line, "%d %s %s %s %s", &s.id, s.name, s.lastname, s.dep, s.course);
if (parsed == 5)
{
// to see the scaned/parsed line
// printf("\nParsed Roll: %d | Name: %s %s | Dept: %s | Course: %s\n", roll, name, lastname, dep, course);
if (s.id == n)
{
printf("\nStudent Found!");
printf("\nRoll No. : %d\nName : %s %s\nDepartment : %s\nCourse : %s", s.id, s.name, s.lastname, s.dep, s.course);
found = 1;
}
}
}
if (!found)
{
printf("\nStudent Not Found ! with Roll No. %d", n);
}
fclose(fp);
pause();
}
// Update data
void updatestd()
{
struct std s;
printf("\n----Edit or Update Student data----\n");
FILE *fp1 = fopen("student.txt", "r");
FILE *fp2 = fopen("temp.txt", "w");
int n, m = 0;
char line[100];
printf("\nEnter Roll no. : ");
scanf("%d", &n);
while (fgets(line, sizeof(line), fp1) != NULL)
{
int parsed = sscanf(line, "%d %s %s %s %s", &s.id, s.name, s.lastname, s.dep, s.course);
if (parsed == 5)
{
if (s.id == n)
{
// Enter Updated Details
printf("\nEnter new Name : ");
scanf("%s", s.name);
printf("\nEnter new LastName : ");
scanf("%s", s.lastname);
printf("\nEnter new Department : ");
scanf("%s", s.dep);
printf("\nEnter new Course : ");
scanf("%s", s.course);
fprintf(fp2, "%d %s %s %s %s\n", s.id, s.name, s.lastname, s.dep, s.course);
m = 1;
}
else
{
// this is for the entire input of the file system.
fputs(line, fp2);
}
}
}
fclose(fp1);
fclose(fp2);
remove("student.txt");
rename("temp.txt", "student.txt");
if (m == 1)
{
printf("\nStudent record Updated succesfully.");
}
else
{
printf("\nStudent with Roll no. %d not found.", n);
}
pause();
}
// Delete data
void deletestd()
{
struct std s;
FILE *fp1 = fopen("student.txt", "r");
FILE *fp2 = fopen("delete.txt", "w");
printf("\n----Delete Student data----\n");
char line[100];
int n, m = 0;
printf("Enter Roll no. : ");
scanf("%d", &n);
while (fgets(line, sizeof(line), fp1) != NULL)
{
int parsed = sscanf(line, "%d %s %s %s %s", &s.id, s.name, s.lastname, s.dep, s.course);
if (parsed == 5)
{
if (s.id == n)
{
m = 1;
}
else
{
// fprintf(fp2, "%d %s %s %s %s\n", s.id, s.name, s.lastname, s.dep, s.course);//safe, cause we can reformat it as we want.
fputs(line, fp2); // not safe as fprintf but faster and shorter.
}
}
}
fclose(fp1);
fclose(fp2);
remove("student.txt");
rename("delete.txt", "student.txt");
if (m == 1)
{
printf("\nStudent record Deleted succesfully.");
}
else
{
printf("\nStudent with Roll no. %d not found.", n);
}
pause();
}
int main()
{
// addStudent();
// viewallstd();
// searchstd();
// updatestd();
// deletestd();
int choice;
do // do while loop is the best suit here the while loop is a bit laggy and controversial. this works perfectly.
{
printf("\n----Student Record System----\n");
printf("1. Add Student\n");
printf("2. View All Students\n");
printf("3. Search Student\n");
printf("4. Update Student's Data\n");
printf("5. Delete Student's Data\n");
printf("6. Exit\n");
printf("Enter Your Choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
addStudent();
break;
case 2:
viewallstd();
break;
case 3:
searchstd();
break;
case 4:
updatestd();
break;
case 5:
deletestd();
break;
case 6:
printf("Exiting....\n");
break;
default:
printf("\nEnter a Valid choice.");
break;
}
} while (choice != 6);
return 0;
}