luc091.c (1582B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Read 'blood donors' file (Name, Address, Age, Blood Type). Print donors with Age < 25 and Blood Type 2. 9 */ 10 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(g) */ 11 12 /* This file is auto-generated by a bot. */ 13 /* This code is not compiled; it is for reference only. */ 14 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <ctype.h> 20 21 struct donor { 22 char name[21]; // 20 cols + null 23 char address[41]; // 40 cols + null 24 int age; // 2 cols -> int 25 int blood_type; // 1 col -> int 26 }; 27 28 void create_donor_file(); 29 30 int main() 31 { 32 FILE *fp; 33 struct donor d; 34 35 create_donor_file(); 36 37 fp = fopen("donors.dat", "rb"); 38 if (!fp) 39 { 40 printf("File error.\n"); 41 exit(1); 42 } 43 44 printf("--- Donors (Age < 25, Type 2) ---\n"); 45 while (fread(&d, sizeof(struct donor), 1, fp) == 1) 46 { 47 if (d.age < 25 && d.blood_type == 2) 48 { 49 printf("Name: %s | Age: %d | Addr: %s\n", d.name, d.age, d.address); 50 } 51 } 52 53 fclose(fp); 54 return 0; 55 } 56 57 void create_donor_file() 58 { 59 struct donor data[] = { 60 {"Amit", "Delhi", 22, 2}, // Match 61 {"Rahul", "Mumbai", 30, 2}, // Old 62 {"Sumit", "Pune", 21, 1}, // Wrong type 63 {"Priya", "Goa", 24, 2} // Match 64 }; 65 FILE *f = fopen("donors.dat", "wb"); 66 fwrite(data, sizeof(struct donor), 4, f); 67 fclose(f); 68 }