-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_making_student_records.c
More file actions
219 lines (168 loc) · 5.82 KB
/
file_making_student_records.c
File metadata and controls
219 lines (168 loc) · 5.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
#if 0
This program show the use of file management in c language.
#endif
#include <stdio.h>
#include <string.h>
#include <windows.h>
void enter_record(FILE *fptr);
void check_record(FILE *fptr);
int main()
{
int choice;
system("cls");
printf("This program store students details and show details when needed \n\n\n");
a:
printf("Select from options below \n\n");
printf("[1]-To enter new records {for first time entry} \n");
printf("[2]-To add new records {for adding to existing records}\n");
printf("[3]-To check records \n");
printf("[4]-To exit \n\n");
printf("Enter option no {like 1} : ");
int n = scanf("%d", &choice);
if (n != 1)
{
printf("\033[5;31m Invalid Input! Only no are allowed \033[0m\n\n");
scanf("%*s");
}
if (choice == 1)
{
// To enter new records
FILE *fptr;
int no_of_students;
fptr = fopen("file7.txt", "w");
if (fptr != NULL)
{
printf("\n\n");
printf("\033[1;32m File open successfully to enter new records \033[0m\n\n");
printf("\033[5;44m Enter students details \033[0m\n\n\n");
printf("Enter the number of students :");
scanf("%d", &no_of_students);
for (int i = 0; i < no_of_students; i++)
{
enter_record(fptr);
}
printf("\033[1;32m All data of %d students recorded successfully \033[0m\n\n", no_of_students);
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
fclose(fptr);
}
else
{
printf("\033[5;31m Error opening the file \033[0m\n\n");
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
}
goto a;
}
else if (choice == 2)
{
// To add new records
FILE *fptr;
int no_of_new_students;
fptr = fopen("file7.txt", "a");
if (fptr != NULL)
{
printf("\n\n");
printf("\033[1;32m File open successfully to add new records \033[0m\n\n");
printf("\033[5;44m Enter students details \033[0m\n\n\n");
printf("Enter the number of students :");
scanf("%d", &no_of_new_students);
for (int i = 0; i < no_of_new_students; i++)
{
enter_record(fptr);
}
printf("\033[1;32m All data of %d students recorded successfully \033[0m\n\n", no_of_new_students);
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
fclose(fptr);
}
else
{
printf("\033[5;31m Error opening the file \033[0m\n\n");
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
}
goto a;
}
else if (choice == 3)
{
// To check records
FILE *fptr;
int no_of_records;
fptr = fopen("file7.txt", "r");
if (fptr != NULL)
{
printf("\n\n");
printf("\033[1;32m File open successfully to read\\check records \033[0m\n\n");
fseek(fptr, 0, SEEK_END);
long fileSize = ftell(fptr);
if (fileSize == 0)
{
printf("\033[5;31m File has no records, Enter records first to check it \033[0m\n\n");
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
goto a;
}
else
{
printf("Enter the no of records you want to check : ");
scanf("%d", &no_of_records);
printf("\n");
printf("\033[5;44m We have these records \033[0m\n\n\n");
printf("Name | City | Marks | Percentage \n");
for (int i = 0; i < no_of_records; i++)
{
check_record(fptr);
}
printf("\033[1;32 \\Records of %d students are fetched and displayed successfully \033[0m\n\n",no_of_records);
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
}
fclose(fptr);
}
else
{
printf("\033[5;31m File not fount! \033[0m\n\n");
printf("--------------------------------------------------------------------------------------------------------\n\n\n");
}
goto a;
}
else if (choice == 4)
{
goto b;
}
else
{
printf("\033[5;31m Invalid option! Enter from {1,2,3,4} only \033[0m\n\n");
goto a;
}
b:
return 0;
}
void enter_record(FILE *fptr)
{
int marks;
float percentage;
char name[100];
char city[50];
printf("Enter your name : ");
scanf(" %[^\n]s", name);
fprintf(fptr, "%s \t", name);
printf("Enter your city : ");
scanf(" %[^\n]s", city);
fprintf(fptr, "%s \t", city);
printf("Enter your marks : ");
scanf("%d", &marks);
fprintf(fptr, "%d \t", marks);
printf("Enter your percentage : ");
scanf("%g", &percentage);
fprintf(fptr, "%g \n", percentage);
printf("\033[1;32m Data of %s recored \033[0m\n\n", name);
}
void check_record(FILE *fptr)
{
// Fetching records from file
int marks;
float percentage;
char name[100];
char city[50];
fscanf(fptr, " %[^\t]s", name);
fscanf(fptr, " %[^\t]s", city);
fscanf(fptr, "%d", &marks);
fscanf(fptr, "%g", &percentage);
printf("\033[1;34m %s | %s | %d | %g \033[0m\n", name, city, marks, percentage);
}