//WAP to store book's name,price and author's name in a file named 'book.dat' and then read and display on //monitor.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k; // pointer for file declaration
char book_name[30],,author[100],choice; //identifier declaration
int book_price; // " "
k=fopen("book.dat","w");
do
{
printf("enter book's name\n"); // getting inuts
gets(book_name);
printf("enter book's price\n");
scanf("%d",&book_price);
printf("enter author\n");
scanf("%s",author);
fprintf(k,"%s %d %s",book_name,book_price,author); //writing data to data file
printf("want to continue y/n\n");
choice=getche(); //getting a character from user
}while(choice!='n'); // validating the input
printf("writing process completed successfully\n");
fclose(k); //closing data file
printf("now reading the records\n");
k=fopen("book.dat","r");
while((fscanf(k,"%s %d %s",book_name,&book_price,author)!=EOF))//reading data from data file
{
printf("book name=%s , price=%d", and author name=%s",book_name,book_price,author_name); //displaying data
}
fclose(k);
getch();
}
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k; // pointer for file declaration
char book_name[30],,author[100],choice; //identifier declaration
int book_price; // " "
k=fopen("book.dat","w");
do
{
printf("enter book's name\n"); // getting inuts
gets(book_name);
printf("enter book's price\n");
scanf("%d",&book_price);
printf("enter author\n");
scanf("%s",author);
fprintf(k,"%s %d %s",book_name,book_price,author); //writing data to data file
printf("want to continue y/n\n");
choice=getche(); //getting a character from user
}while(choice!='n'); // validating the input
printf("writing process completed successfully\n");
fclose(k); //closing data file
printf("now reading the records\n");
k=fopen("book.dat","r");
while((fscanf(k,"%s %d %s",book_name,&book_price,author)!=EOF))//reading data from data file
{
printf("book name=%s , price=%d", and author name=%s",book_name,book_price,author_name); //displaying data
}
fclose(k);
getch();
}