bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / letusc / luc091.c

luc091.c (1391B)


      1 /* Read 'blood donors' file (Name, Address, Age, Blood Type). Print donors with Age < 25 and Blood Type 2.
      2 */
      3 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(g) */
      4 
      5 /* This file is auto-generated by a bot. */
      6 /* This code is not compiled; it is for reference only. */
      7 
      8 
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <ctype.h>
     13 
     14 struct donor {
     15     char name[21];    // 20 cols + null
     16     char address[41]; // 40 cols + null
     17     int age;          // 2 cols -> int
     18     int blood_type;   // 1 col -> int
     19 };
     20 
     21 void create_donor_file();
     22 
     23 int main()
     24 {
     25     FILE *fp;
     26     struct donor d;
     27 
     28     create_donor_file();
     29 
     30     fp = fopen("donors.dat", "rb");
     31     if (!fp)
     32     {
     33         printf("File error.\n");
     34         exit(1);
     35     }
     36 
     37     printf("--- Donors (Age < 25, Type 2) ---\n");
     38     while (fread(&d, sizeof(struct donor), 1, fp) == 1)
     39     {
     40         if (d.age < 25 && d.blood_type == 2)
     41         {
     42             printf("Name: %s | Age: %d | Addr: %s\n", d.name, d.age, d.address);
     43         }
     44     }
     45 
     46     fclose(fp);
     47     return 0;
     48 }
     49 
     50 void create_donor_file()
     51 {
     52     struct donor data[] = {
     53         {"Amit", "Delhi", 22, 2},   // Match
     54         {"Rahul", "Mumbai", 30, 2}, // Old
     55         {"Sumit", "Pune", 21, 1},   // Wrong type
     56         {"Priya", "Goa", 24, 2}     // Match
     57     };
     58     FILE *f = fopen("donors.dat", "wb");
     59     fwrite(data, sizeof(struct donor), 4, f);
     60     fclose(f);
     61 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror