-->
Showing posts with label book_name and book_price of some books.. Show all posts
Showing posts with label book_name and book_price of some books.. Show all posts

program to store book_id,book_name and book_price of some books.

using codeblocks
--------------------------------------------------------------------------------------------------------
/*program to store book_id,book_name and book_price of some books to a data file
 until you say 'n'. Then same file displays all records.
*/
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
struct book                           //struct tag
{
    int book_id;
    float book_price;
    char book_name[50];
}var;                                      //variable
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                             //identifier declaration
k=fopen("book.txt","w");                  //file opening in write mode
do
{
printf("\n enter book id\n");
scanf("%d",&var.book_id);
printf("enter book price\n");
scanf("%f",&var.book_price);
printf("enter  book name\n");           // getting inputs
scanf("%s",var.book_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------\n");
printf("reading data\n");             //opening file in read mode
k=fopen("book.txt","r");               //file opening

while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
                                        //As it becomes less than 1, it stops
{
printf("book id=%d,book price=%f,book name=%s\n",var.book_id,var.book_price,var.book_name);//prints the struct value
}
fclose(k);                            //closing of file.
getch();
return 0;
}






//or
/*program to store book_id,book_name and book_price of some books to a data file
 until you say 'n'. Then same file displays all records.

#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
struct book
{
    int book_id;
    float book_price;
    char book_name[50];
}var;
int main()
{
FILE *k;                                  //  pointer for file declaration
char choice;                               //identifier declaration
k=fopen("book.txt","w");                  //file opening in write mode
do
{
printf("\n enter book id\n");
scanf("%d",&var.book_id);
printf("enter book price\n");
scanf("%f",&var.book_price);
printf("enter  book name\n");           // getting inputs
scanf("%s",var.book_name);
fprintf(k,"%d %f %s\n",var.book_id,var.book_price,var.book_name); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character
}while(choice!='n');        //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------\n");
printf("reading data\n");
k=fopen("book.txt","r");//file opening

while((fscanf(k,"%d %f %s",&var.book_id,&var.book_price,var.book_name))!=EOF)
                                        // fscanf  reads the records till it is the last one.
                                        //As it ends, it stops
{
printf("book id=%d,book price=%f,book name=%s\n",var.book_id,var.book_price,var.book_name);//prints the struct value
}
fclose(k);                            //closing of file.
getch();
return 0;
}
*/