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
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s[100];
int n, i;
FILE *fp;
printf("Enter number of students: ");
scanf("%d", &n);
// Input records
for (i = 0; i < n; i++) {
printf("Enter ID, Name, Marks: ");
scanf("%d %s %f", &s[i].id, s[i].name, &s[i].marks);
}
// Write to file
fp = fopen("students.dat", "wb");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
fwrite(s, sizeof(struct Student), n, fp);
fclose(fp);
// Read from file
fp = fopen("students.dat", "rb");
if (fp == NULL) {
printf("Error opening file\n");
return 1;
}
printf("\nRecords from file:\n");
for (i = 0; i < n; i++) {
fread(&s[i], sizeof(struct Student), 1, fp);
printf("ID: %d, Name: %s, Marks: %.2f\n", s[i].id, s[i].name, s[i].marks);
}
fclose(fp);
return 0;
}Coding works best on desktop or with an external keyboard.