Pages

Friday, 9 February 2018

A hospital keeps a file of blood donors in which each record has the format: Name: 20 Columns Address: 40 Column Age: 2 Columns Blood Type: 1 Column (Type 1, 2, 3 or 4) Write a program to read the file and print a list of all blood donors whose age is below 25 and blood is type 2.



A hospital keeps a file of blood donors in which each record has the format:
Name: 20 Columns
Address: 40 Column
Age: 2 Columns
Blood Type: 1 Column (Type 1, 2, 3 or 4)
Write a program to read the file and print a list of all blood donors whose age is below 25 and blood is type 2.

#include<stdio.h>
#include<conio.h>
void main() {

FILE *fp;
char another='y';

struct blood {
    char name[50];
    char adr[50];
    int age;
    int bld;
    } b;

clrscr();

fp=fopen("BLOODBANK.DAT","wb");

if(fp==NULL) {
printf("cannot open target file!\n");
exit();
}

while(another=='Y' || another=='y') {

clrscr();
printf("\t\tInformation of Blood donor\n");
printf("\t\t**************************\n\n\n");
printf("Enter the name: ");
scanf("%s",b.name);
printf("\n\nenter the address: ");
scanf("%s",b.adr);
printf("\n\nenter the age: ");
scanf("%d",&b.age);
printf("\n\nenter the blood group(1/2/3/4): ");
scanf("%d",&b.bld);

fprintf(fp,"%s\t%s\t%d\t%d",b.name,b.adr,b.age,b.bld);

printf("\n\n\nenter more information(Y/N): ");
fflush(stdin);

another=getch();

}

fclose(fp);

}
Program to read record file for specifications




/* This program will read the information from the file made by writing program */

           /* Reading Program */

#include<stdio.h>
#include<conio.h>
void main() {

FILE *fp;
char ch;

struct blood {
        char name[50];
        char adr[50];
        int age;
        int bld;
        }b;
clrscr();

fp=fopen("BLOODBANK.DAT","rb");

if(fp==NULL) {
printf("cannot open source file!\n\n");
exit();
}

while(fscanf(fp,"%s\t%s\t%d\t%d",&b.name,&b.adr,&b.age,&b.bld)!=EOF)
if(b.age<25 && b.bld==2) {
printf("\n%s\t %s\t%2d\t %d",b.name,b.adr,b.age,b.bld);
}
fclose(fp);

getch();
}