Mode:
Duration:
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int id;
char name[50];
float marks;
};
void addRecord(FILE *fp) {
struct Student s;
printf("Enter ID: ");
scanf("%d", &s.id);
printf("Enter Name: ");
scanf("%s", s.name);
printf("Enter Marks: ");
scanf("%f", &s.marks);
fwrite(&s, sizeof(s), 1, fp);
}
void displayRecords(FILE *fp) {
struct Student s;
rewind(fp);
while (fread(&s, sizeof(s), 1, fp)) {
printf("ID: %d, Name: %s, Marks: %.2f\n", s.id, s.name, s.marks);
}
}
void searchRecord(FILE *fp, int id) {
struct Student s;
rewind(fp);
while (fread(&s, sizeof(s), 1, fp)) {
if (s.id == id) {
printf("Found -> ID: %d, Name: %s, Marks: %.2f\n", s.id, s.name, s.marks);
return;
}
}
printf("Record not found\n");
}
void updateRecord(FILE *fp, int id) {
struct Student s;
rewind(fp);
while (fread(&s, sizeof(s), 1, fp)) {
if (s.id == id) {
printf("Enter new name and marks: ");
scanf("%s %f", s.name, &s.marks);
fseek(fp, -sizeof(s), SEEK_CUR);
fwrite(&s, sizeof(s), 1, fp);
printf("Record updated\n");
return;
}
}
printf("Record not found\n");
}
int main() {
FILE *fp = fopen("records.dat", "rb+\");
if (fp == NULL) {
fp = fopen("records.dat", "wb+");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
}
int choice, id;
while (1) {
printf("\n1. Add\n2. Display\n3. Search\n4. Update\n5. Exit\nChoice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addRecord(fp);
break;
case 2:
displayRecords(fp);
break;
case 3:
printf("Enter ID: ");
scanf("%d", &id);
searchRecord(fp, id);
break;
case 4:
printf("Enter ID: ");
scanf("%d", &id);
updateRecord(fp, id);
break;
case 5:
fclose(fp);
return 0;
default:
printf("Invalid choice\n");
}
}
}Coding works best on desktop or with an external keyboard.