- Add book details
- Search a book details for a give ISBN and display book details, if available.
- Update a book details using ISBN
- Delete book details for a given ISBN and display list of remaining Books.
#include <stdio.h>
struct Book
{
long ISBN;
char Title[20];
char Author[20];
float Price;
int Pages;
char Publisher[20];
};
FILE *file;
void add();
void search();
void update();
void delete();
void display();
void input(struct Book*);
int main()
{
int choice;
do
{
printf("Main Menu\n");
printf("1.Add Book\n");
printf("2.Search\n");
printf("3.Update\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: add();
break;
case 2: search();
break;
case 3: update();
break;
case 4: delete();
break;
case 5: display();
break;
}
}while(choice != 6);
return 0;
}
void add()
{
struct Book data;
file = fopen("books.dat", "ab");
printf("Please enter Book details : \n");
input(&data);
fwrite(&data, sizeof(data), 1, file);
fclose(file);
printf("Book details added \n\n");
}
void input(struct Book *b)
{
printf("Enter ISBN : ");
scanf("%ld", &b->ISBN);
fflush(stdin);
printf("Enter Title : ");
gets(b->Title);
printf("Enter Author : ");
gets(b->Author);
printf("Enter price : ");
scanf("%f", &b->Price);
printf("Enter pages : ");
scanf("%d", &b->Pages);
printf("Enter Publisher : ");
gets(b->Publisher);
}
void display()
{
struct Book data;
printf("\n\nDISPLAY ALL RECORD !!!\n");
file = fopen("student.dat", "rb");
while((fread(&data, sizeof(data), 1, file)) > 0)
{
printf("ISBN number : %ld \n", data.ISBN);
printf("Title : %s \n", data.Title);
printf("Author : %s \n", data.Author);
printf("Price : %f \n", data.Price);
printf("Pages : %d \n", data.Pages);
printf("Publisher : %s \n", data.Publisher);
printf("====================================\n\n");
}
fclose(file);
}
void update()
{
struct Book data;
long num;
int found=0;
printf("Enter ISBN number : ");
scanf("%ld", &num);
file = fopen("student.dat", "rb+");
while((fread(&data, sizeof(data), 1, file)) > 0 && found == 0)
{
if(data.ISBN == num)
{
printf("Please Enter New details : \n");
input(&data);
fseek(file, -(long)sizeof(data), 1);
fwrite(&data, sizeof(data), 1, file);
printf("Record Updated\n\n");
found = 1;
}
}
fclose(file);
if(!found)
printf("Record Not Found \n\n");
}
void search()
{
struct Book data;
long num;
int found=0;
printf("Enter ISBN number : ");
scanf("%ld", &num);
file = fopen("student.dat", "rb+");
while((fread(&data, sizeof(data), 1, file)) > 0 && found == 0)
{
if(data.ISBN == num)
{
printf("ISBN number : %ld \n", data.ISBN);
printf("Title : %s \n", data.Title);
printf("Author : %s \n", data.Author);
printf("Price : %f \n", data.Price);
printf("Pages : %d \n", data.Pages);
printf("Publisher : %s \n", data.Publisher);
printf("====================================\n\n");
found = 1;
}
}
fclose(file);
if(!found)
printf("Record Not Found \n\n");
}
void delete()
{
long num;
struct Book data;
FILE *file2;
printf("Please Enter ISBN number : ");
scanf("%ld", &num);
file = fopen("books.dat", "rb");
file2 = fopen("temp.dat", "wb");
rewind(file);
while((fread(&data, sizeof(data), 1, file)) > 0)
{
if (data.ISBN != num)
{
fwrite(&data, sizeof(data), 1, file2);
}
}
fclose(file2);
fclose(file);
remove("book.dat");
rename("temp.dat", "book.dat");
printf("Record deleted\n\n");
}