Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- struct bookinfo
- {
- char title[50],author[50];
- int isbn,price;
- };
- struct Node
- {
- struct bookinfo book;
- struct Node *next;
- }*Head;
- struct bookinfo getBook()
- {
- char name[50],auth[50];
- int cost,sr;
- struct bookinfo tempbook;
- printf("Enter ISBN Number : ");
- scanf("%d",&sr);
- printf("Enter Book Name : ");
- scanf("%s", name);
- printf("Enter Book Author : ");
- scanf("%s",auth);
- printf("Enter Price : ");
- scanf("%d",&cost);
- tempbook.isbn = sr;
- strcpy(tempbook.title,name);
- strcpy(tempbook.author,auth);
- tempbook.price = cost;
- return(tempbook);
- }
- void addBook()
- {
- long int recsize;
- struct bookinfo newbook = getBook();
- struct Node *temp1, *temp2;
- temp1=(struct Node *)malloc(sizeof(struct Node));
- temp1->book = newbook;
- // Copying the Head location into another node.
- temp2=Head;
- if(Head == NULL)
- {
- // If List is empty we create First Node.
- Head=temp1;
- Head->next=NULL;
- }
- else
- {
- // Traverse down to end of the list.
- while(temp2->next != NULL)
- temp2=temp2->next;
- // Append at the end of the list.
- temp1->next=NULL;
- temp2->next=temp1;
- }
- system("clear");
- printf("\nBook Added Successfully\n");
- FILE *fp,*fs;
- fp = fopen("bookrecord.txt","a");
- if (fp == NULL)
- {
- printf("Cannot open file");
- }
- fprintf ( fp, "\n %d \n %s \n %s \n %d\n", newbook.isbn, newbook.title, newbook.author, newbook.price ) ;
- fflush (stdin) ;
- fclose(fp);
- fs = fopen( "BOOK.DAT", "rb+" ) ;
- if ( fs == NULL )
- {
- fs = fopen ( "BOOK.DAT", "wb+" ) ;
- if ( fs == NULL )
- {
- puts ( "Cannot open file" ) ;
- }
- }
- recsize = sizeof (newbook) ;
- fseek ( fs, 0 , SEEK_END ) ;
- fwrite ( &newbook, recsize, 1, fs ) ;
- fflush (stdin) ;
- fclose(fs);
- }
- void listFile()
- {
- long int recsize;
- struct bookinfo b1;
- recsize = sizeof ( b1 ) ;
- FILE *fp;
- fp = fopen("BOOK.DAT","rb+");
- if (fp == NULL)
- {
- printf("Cannot open file");
- }
- rewind (fp) ;
- while ( fread ( &b1, recsize, 1, fp ) == 1 )
- {printf ( "\n ISBN:%d \n Title:%s \n Author:%s \n Price:%d\n", b1.isbn, b1.title, b1.author, b1.price ) ;}
- fclose(fp);
- }
- void display()
- {
- struct Node *cur_ptr;
- cur_ptr=Head;
- if(cur_ptr==NULL)
- {
- system("clear");
- printf("\nThere are No BOOKS in the List\n\n");
- }
- else
- {
- system("clear");
- printf("\nBooks : \n\n");
- //traverse the entire linked list
- while(cur_ptr!=NULL)
- {
- printf("ISBN : %d \n",cur_ptr->book.isbn);
- printf("Title : %s \n",cur_ptr->book.title);
- printf("Author : %s \n",cur_ptr->book.author);
- printf("Price : %d \n",cur_ptr->book.price);
- cur_ptr=cur_ptr->next;
- printf("\n");
- }
- printf("\n");
- }
- listFile();
- }
- int delBook(int num)
- {
- int flag = 0,error = 440;
- struct Node *prev_ptr, *cur_ptr;
- cur_ptr=Head;
- while(cur_ptr != NULL)
- {
- if(cur_ptr->book.isbn == num)
- {
- if(cur_ptr==Head)
- {
- Head=cur_ptr->next;
- free(cur_ptr);
- system("clear");
- printf("\n Book Removed\n\n");
- flag = 1;
- return(1);
- break;
- }
- else
- {
- prev_ptr->next=cur_ptr->next;
- free(cur_ptr);
- system("clear");
- printf("\n Book Removed\n\n");
- flag = 1;
- return(1);
- break;
- }
- }
- else
- {
- prev_ptr=cur_ptr;
- cur_ptr=cur_ptr->next;
- }
- if(flag == 0){
- flag = flag + error;
- system("clear");
- printf("\nBook(ISBN %d) not found in the List\n\n", num);
- }
- }
- return(flag);
- }
- int search(int num)
- {
- int flag = 0,error=404;
- struct Node *prev_ptr, *cur_ptr;
- cur_ptr=Head;
- while(cur_ptr != NULL)
- {
- if(cur_ptr->book.isbn == num)
- {
- system("clear");
- printf("\nBook Found \n\n");
- printf("ISBN : %d \n",cur_ptr->book.isbn);
- printf("Title : %s \n",cur_ptr->book.title);
- printf("Author : %s \n",cur_ptr->book.author);
- printf("Price : %d \n",cur_ptr->book.price);
- cur_ptr=cur_ptr->next;
- printf("\n");
- flag = 1;
- return(1);
- break;
- }
- else
- {
- prev_ptr=cur_ptr;
- cur_ptr=cur_ptr->next;
- }
- if(flag == 0){
- flag = flag + error;
- system("clear");
- printf("\nBook(ISBN %d) not found in the List\n\n", num);
- }
- }
- return(flag);
- }
- void menu()
- {
- printf("\nMenu : ");
- printf("\n**********************************************************************");
- printf("\nEnter 1 to Add New Book");
- printf("\nEnter 2 to Remove Existing Book");
- printf("\nEnter 3 to List All Books");
- printf("\nEnter 4 to Search a Book");
- printf("\nEnter 0 to Quit");
- printf("\n************************************************************************");
- }
- void main()
- {
- system("clear");
- int success;
- int found;
- int delISBN;
- int searchISBN;
- int choice;
- do
- {
- menu();
- printf("\nEnter your choice : ");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- addBook();break;
- case 2:
- printf("Enter ISBN to Delete Book : ");
- scanf("%d",&delISBN);
- success = delBook(delISBN);
- if(success != 440 && success != 1){printf("\nBook Not Available\n");success = 9;}
- break;
- case 3:
- display();
- break;
- case 4:
- printf("Enter ISBN to Search Book : ");
- scanf("%d",&searchISBN);
- found = search(searchISBN);
- if(found != 404 && found != 1){printf("\nBook Not Available\n");found = 10;}
- break;
- case 0:
- printf("\nThank You for using our Service\n\n");
- break;
- default:
- printf("Please Enter a Valid Choice !");
- }
- }while(choice!=0);
- }
Add Comment
Please, Sign In to add comment