c programming ( putty )

profileQ87h
assigntemplate.c

#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct book_type { char title[100],authorFirstName[100],authorLastName[100]; int year; float rCost; }node; // This function loads a catalogue of books from a user specified file name. // Note you need to use malloc inside this function to allocate the memory // needed for the catalogue of books. // After catalogue is allocated and filled up with books loaded from the file, // it is returned back to the main program via the book_type * return type. // The pointer parameter numBooks is used to pass the number of books back // out to the numBooks variable in the main function. This way the other // functions will be able to use this variable to find how many books // are stored in the catalogue. struct book_type * loadCatalogue(int * numBooks); // This function saves catalogue into a user specified text file void saveCatalogue(struct book_type * bCatalogue, int numBooks); // This function displays the catalogue onto the screen. void displayCatalogue(struct book_type * bCatalogue, int numBooks); // This function finds a user specified book or set of books and displays them void findBook(struct book_type * bCatalogue, int numBooks); //This function finds a user specified book or set of books and deletes them struct book_type * deleteBook(struct book_type * bCatalogue, int numBooks); int main() { struct book_type * bookCatalogue=NULL; int numBooks,choice=0; //This loop goes on untill the user gives 6 as a choice while(choice!=6){ printf("\nChoose one of the following options:\n1. Load catalogue from file\n2. Save catalogue to file\n3. Display catalogue\n4. Find book from catalogue\n5. Delete book from catalogue\n6. Quit\n"); scanf("%d",&choice); fflush(stdin); switch(choice){ case 1: bookCatalogue = loadCatalogue(&numBooks);//calls the load catalogue function break; case 2: saveCatalogue(bookCatalogue, numBooks);//calls the function to save the catalogue break; case 3: displayCatalogue(bookCatalogue, numBooks); // calls the function to display the catalogue on screen break; case 4: findBook(bookCatalogue, numBooks); //calls the function to search for a book in a catalogue break; case 5: bookCatalogue = deleteBook(bookCatalogue, numBooks); //calls the function to delete a book from the catalogue break; case 6: printf("Program will exit now\n"); break; default: printf("Wrong option selected, please select again\n"); // If user gives an invalid input, ask him to re-enter break; } } return 0; } struct book_type * loadCatalogue(int * numBooks) { struct book_type *bCatalogue; char fileName[100],line[100]; int i=0; FILE *fp; bCatalogue=(node *)malloc(sizeof(node)); //allocate memory printf("Enter the name of the file to open:\n"); gets(fileName);// Take filename as input //fflush(stdin); fp=fopen(fileName,"r");//opens the file in read only mode if(fp==NULL){//If file cannot be opened due to some reason than print the error perror(fileName); return NULL; } while ( fgets ( line, sizeof(line), fp ) != NULL ) /* read a line */ { //First line represents the number of books if(!i) { sscanf(line,"%d",&numBooks); continue; } //Take different attributes of book and place them in proper variables. if((i-1)%4==2) { sscanf(line,"%s",bCatalogue[i/4].title); } //Since first name and last name are in the same line, we need to split it from ',' else if((i-1)%4==3) { char fName[100],lName[100]; int j; for(j=0;line[j]!=',';j++) { fName[j]=line[j]; } fName[j++]='\0'; for(;line[j]!='\0';j++) { lName[j]=line[j]; } lName[j]='\0'; sscanf(fName,"%s",bCatalogue[i/4].authorFirstName); sscanf(fName,"%s",bCatalogue[i/4].authorLastName); } else if((i-1)%4==0) { sscanf(line,"%d",&bCatalogue[i/4].year); } else if((i-1)%4==1) { sscanf(line,"%f",&bCatalogue[i/4].rCost); } i++; } fclose ( fp ); return bCatalogue; } void saveCatalogue(struct book_type * bCatalogue, int numBooks) { if(bCatalogue==NULL) printf("Please load the the book catalogue first\n"); else { char fileName[100], name[200],output[20]; FILE *fp=NULL; int i=0,j=0,k=0; printf("Enter the name of the file to save:\n"); gets(fileName); fp=fopen(fileName,"w");//open a new file in write only mode if(fp==NULL){ perror(fileName); } else { sprintf(output,"%d",numBooks); fputs(output,fp); fwrite("\n", sizeof(char), 1, fp); while(i<4*numBooks) { fputs(bCatalogue[i/4].title,fp); fwrite("\n", sizeof(char), 1, fp); for(;bCatalogue[i/4].authorFirstName[j]!='\0';j++) name[j]=bCatalogue[i/4].authorFirstName[j]; name[j]=','; name[++j]=' '; for(;bCatalogue[i/4].authorLastName[k]!='\0';k++,j++) name[j]=bCatalogue[i/4].authorLastName[k]; name[j]='\0'; fputs(name,fp); fwrite("\n", sizeof(char), 1, fp); sprintf(output,"%d",bCatalogue[i/4].year); fputs(output,fp); fwrite("\n", sizeof(char), 1, fp); sprintf(output,"%f",bCatalogue[i/4].rCost); fputs(output,fp); fwrite("\n", sizeof(char), 1, fp); i++; } } fclose(fp);//close the file } } void displayCatalogue(struct book_type * bCatalogue, int numBooks) { if(bCatalogue==NULL) printf("Please load the the book catalogue first\n"); else { int i=0; printf("Number of books in catalogue = %d\n---------------------------------------------------------------\n",numBooks); while(i<numBooks) { printf("Title: %s\nAuthor: %s %s\nYear of publication: %d\nReplacement cost: $%f\n---------------------------------------------------------------\n",bCatalogue[i].title,bCatalogue[i].authorFirstName,bCatalogue[i].authorLastName,bCatalogue[i].year,bCatalogue[i].rCost); i++; } } } void findBook(struct book_type * bCatalogue, int numBooks) { if(bCatalogue==NULL) printf("Please load the the book catalogue first\n"); else { int flag=0,i=0,choice=0; char find[100]; printf("\nChoose one of the following options:\n1. Specify book title\n2. Specify author first name\n3. Specify author last name\n"); scanf("%d",&choice); if(choice==1) { printf("Enter book title you want to search for\n"); gets(find); while(i<numBooks) { if(!(strcmpi(find,bCatalogue[i].title))) { displayCatalogue(&bCatalogue[i],1); flag=1; } i++; } } else if(choice==2){ printf("Enter author first name that you want to search for\n"); gets(find); while(i<numBooks) { if(!(strcmpi(find,bCatalogue[i].authorFirstName))) { displayCatalogue(&bCatalogue[i],1); flag=1; } i++; } } else if(choice==3){ printf("Enter author last name that you want to search for\n"); gets(find); while(i<numBooks) { if(!(strcmpi(find,bCatalogue[i].authorLastName))) { displayCatalogue(&bCatalogue[i],1); flag=1; } i++; } } else printf("Wrong choice\n"); if(flag==0) printf("No search result found\n"); } } struct book_type * deleteBook(struct book_type * bCatalogue, int numBooks) { if(bCatalogue==NULL) printf("Please load the the book catalogue first\n"); else { int flag=0,i=0,j=0,choice; char find[100]; struct book_type *bCatalogue1; bCatalogue1=(node *)malloc(sizeof(node)); printf("\nChoose one of the following options:\n1. Specify book title\n2. Specify author first name\n3. Specify author last name\n"); scanf("%d",&choice); if(choice==1) { printf("Enter book title you want to search for\n"); gets(find); while(i<numBooks) { if(!(strcmpi(find,bCatalogue[i].title))) { displayCatalogue(&bCatalogue[i],1); flag=1; } else { bCatalogue1[j]=bCatalogue[i];//add book if the user doesnot specified this book to be deleted j++; } i++; } } else if(choice==2){ printf("Enter author first name that you want to search for\n"); gets(find); while(i<numBooks) { if(!(strcmpi(find,bCatalogue[i].authorFirstName))) { displayCatalogue(&bCatalogue[i],1); flag=1; } else { bCatalogue1[j]=bCatalogue[i];//add book if the user doesnot specified this book to be deleted j++; } i++; } } else if(choice==3){ printf("Enter author last name that you want to search for\n"); gets(find); while(i<numBooks) { if(!(strcmpi(find,bCatalogue[i].authorLastName))) { displayCatalogue(&bCatalogue[i],1); flag=1; } else { bCatalogue1[j]=bCatalogue[i];//add book if the user doesnot specified this book to be deleted j++; } i++; } } else printf("Wrong choice\n"); if(flag==0) printf("No search result found\n"); return bCatalogue1; } return NULL; }