Code:
#include <stdio.h>
struct record{
char book_name[50];
char author[50];
char publisher[50];
int year;
int edition;
float price;
};
/*
void add_record(char b_name,char author, char publisher, int year, int
edition, float price)
{
*/
main(){
FILE *fp;
struct record r[100];
fp=fopen("file1.txt","r");
char category;
int counter=0;
char book_search,author_search,publisher_search;
int i=0;
int year_search,edition_search;
float price_search;
/*
while(counter<2){
printf("Enter the name,author,publisher,year: ");
*/
while(
fscanf (fp,"%s%s%s%d%d%f",
r[counter].book_name,
r[counter].author,
r[counter].publisher,
&(r[counter].year),
&(r[counter].edition),
&(r[counter].price))==6
)
{
counter++;
}
/*
printf(" the first record is %s\t%s\t%s\t%d\t%d\t%f\n"
,r[0].book_name,r[0].author,r[0].publisher,
r[0].year, r[0].edition, r[0].price);
printf(" the fourth author is %s\n" , r[3].author);
printf(" The number of records is %d \n" , counter);
*/
printf("Type b,a,u,y,e,or p to search for book,author,publisher,year, edition, or price: " );
scanf("%s", &category);
switch(category){
case 'b':
printf("Type the name of the book: ");
scanf("%s", &book_search);
while(i<counter){
if(strcmp( r[i].book_name,&book_search)==0){
printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
,r[i].book_name,r[i].author,r[i].publisher,
r[i].year, r[i].edition, r[i].price);
}
i++;
}
i=0;
break;
case 'a':
printf("Type the author of the book: ");
scanf("%s", &author_search);
while(i<counter){
if(strcmp(&author_search, r[i].author)==0){
printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
,r[i].book_name,r[i].author,r[i].publisher,
r[i].year, r[i].edition, r[i].price);
}
i++;
}
i=0;
break;
case 'u':
printf("Type the publisher of the book: ");
scanf("%s", &publisher_search);
while(i<counter){
if(strcmp(&publisher_search, r[i].publisher)==0){
printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
,r[i].book_name,r[i].author,r[i].publisher,
r[i].year, r[i].edition, r[i].price);
}
i++;
}
i=0;
break;
case 'y':
printf("Type the year of the book: ");
scanf("%d", &year_search);
while(i<counter){
if(year_search==r[i].year){
printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
,r[i].book_name,r[i].author,r[i].publisher,
r[i].year, r[i].edition, r[i].price);
}
i++;
}
i=0;
break;
case 'e':
printf("Type the edition of the book: ");
scanf("%d", &edition_search);
while(i<counter){
if(edition_search==r[i].edition){
printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
,r[i].book_name,r[i].author,r[i].publisher,
r[i].year, r[i].edition, r[i].price);
}
i++;
}
i=0;
break;
case 'p':
printf("Type the price of the book: ");
scanf("%f", &price_search);
while(i<counter){
if(price_search==r[i].price){
printf(" %s\t%s\t%s\t%d\t%d\t%f\n"
,r[i].book_name,r[i].author,r[i].publisher,
r[i].year, r[i].edition, r[i].price);
}
i++;
}
i=0;
break;
}
fclose(fp);
}