Question #55835

A hospital keeps a file of blood donors in which each record
has the format:
Name:
Address:
Age:
Blood Type: (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.
1

Expert's answer

2016-02-10T00:01:18-0500

Answer on Question #55835-Engineering-Software Engineering

A hospital keeps a file of blood donors in which each record has the format:

Name:

Address:

Age:

Blood Type: (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.

Solution

Program to create record file of blood donors


/* This program will make a file of blood donors and save information in it */
/* Writing program */
#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\nInformation 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();
}


https://www.AssignmentExpert.com

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!
LATEST TUTORIALS
APPROVED BY CLIENTS