Skip to content

luc091.c

Problem Statement

Read 'blood donors' file (Name, Address, Age, Blood Type). Print donors with Age < 25 and Blood Type 2.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)

Actions

Raw View on GitHub

💡 You can print or save this file by opening Raw and using your browser.

Source Code

#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);
}