luc091.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Read 'blood donors' file (Name, Address, Age, Blood Type). Print donors with Age < 25 and Blood Type 2.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct donor {
char name[21]; // 20 cols + null
char address[41]; // 40 cols + null
int age; // 2 cols -> int
int blood_type; // 1 col -> int
};
void create_donor_file();
int main()
{
FILE *fp;
struct donor d;
create_donor_file();
fp = fopen("donors.dat", "rb");
if (!fp)
{
printf("File error.\n");
exit(1);
}
printf("--- Donors (Age < 25, Type 2) ---\n");
while (fread(&d, sizeof(struct donor), 1, fp) == 1)
{
if (d.age < 25 && d.blood_type == 2)
{
printf("Name: %s | Age: %d | Addr: %s\n", d.name, d.age, d.address);
}
}
fclose(fp);
return 0;
}
void create_donor_file()
{
struct donor data[] = {
{"Amit", "Delhi", 22, 2}, // Match
{"Rahul", "Mumbai", 30, 2}, // Old
{"Sumit", "Pune", 21, 1}, // Wrong type
{"Priya", "Goa", 24, 2} // Match
};
FILE *f = fopen("donors.dat", "wb");
fwrite(data, sizeof(struct donor), 4, f);
fclose(f);
}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
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