Question: I'm doing something wrong in my code. It asks to enter a file name, but it does not print the book list, but it asks
I'm doing something wrong in my code. It asks to enter a file name, but it does not print the book list, but it asks which method of search I want to do.
I added to code below.
In your main.c, define a structure called book (follow the bank_record example) that contains:
- char title[255];
- char author_name[50];
- char ISBN[10];
- int pages;
- int year_published;
- In your main ( ), create a 360-element array of book-structures. These will be populated in a later step.
- Example single instance of a blank structure: book my_book;
- Example array of blank structures: book my_book_array [50];
- Create a function called parse_file ( ) that takes a parameter of filename and an array of book structures to populate. Call this function in your main ( ) to populate your array of books.
- This function should open the desired file and loop through each line, scanning book information and populating new instances of the book structure array.
- Each line holds one book record, separated by commas.
- Consider using the strtok ( ) function from the
library to tokenize (parse) a line by a comma delimiter. - Line format: Book Title, Author Name, ISBN, Number of pages, Year published
- NOTE: If you followed the typedef struct format described in the introduction of this lab, passing a structure array to a function is accomplished the same way as passing any other type of array to functions.
- NOTE: Some records may be missing information, such as ISBN or page numbers. They will be N/A in the BookList.csv, make sure to handle that string where your program may be expecting an integer.
- NOTE: A skeleton of this function has been provided to you in Appendix-B of this lab handout.
- This function should open the desired file and loop through each line, scanning book information and populating new instances of the book structure array.
- Create a function called print_book ( ) that takes a book as a parameter. The function should neatly print the contents of that books structure to the terminal window. Remember: some records might have null parameters, make sure to handle that in your output.
- Create a function called search_title ( ) that takes a book array, the number of books in that book array, and a character string title as parameters. This search function should loop through all of the books in the array in search of a title that matches with the passed character string, printing to the terminal any books that fulfill this (there could be more than 1 match).
- Create a function called search_author ( ) that takes a book array, the number of books in that book array, and a character string author as parameters. This search function should loop through all of the books in the array in search of an author (first and last name) that matches with the passed character string, printing to the terminal any books that fulfill this (there could be more than 1 match).
- Create a function called search_ISBN ( ) that takes a book array, the number of books in that book array, and an character string ISBN as parameters. This search function should loop through all of the books in the array in search of an ISBN that matches with the ISBN passed, printing to the terminal any books that fulfill this (there could be more than 1 match).
- In your main(), create a while-loop that monitors user input for an integer. The integer input will act as a search by selection to offer the following search features:
- [0] Search by Title
- [1] Search by Author Name
- [2] Search by ISBN
BE SURE TO ERROR CHECK USER INPUT!!
- Based on the selected search by criteria, accept appropriate input from the user and call the correct search function to print out the book(s) that match the users search. If there are multiple matches, your program should print multiple books
#include
typedef struct{ char title[255]; char author_name[50]; char ISBN[10]; int pages; int year_published; } books;
//PROTOTYPES int parse_file(char filename[], books my_book_array[]); void print_book(books my_book_array[], int n); void search_title(books my_book_array[], int n, char title); void search_author(books my_book_array[], int n, char author_name); void search_ISBN(books my_book_array[], int n, char ISBN);
int main() { int n, option; char file[100]; books my_book_array[360]; char title[255]; char author_name[50]; char ISBN[10];
//OBTAINING DESIRED FILE printf("Enter the name of the file: "); scanf("%s",&file);
n=parse_file(file, my_book_array);
//PRINTING BOOKS printf("The Book List "); print_book(my_book_array,n);
//SEARCH OPTIONS while(1){//while true printf("[0] Search by Title "); printf("[1] Search by Author Name "); printf("[2] Search by ISBN "); printf("Choose option: "); scanf("%d", &option);
if( option ==0){ printf("Enter title: "); fflush(stdin); fgets(title,256,stdin); title[strlen(title)-1] = '\0'; search_title(my_book_array,n,title); } if( option ==1){ printf("Enter author's name: "); fflush(stdin); fgets(author_name,51,stdin); author_name[strlen(author_name)-1] = '\0'; search_author(my_book_array,n,author_name); } if( option ==2){ printf("Enter ISBN: "); fflush(stdin); fgets(ISBN,11,stdin); ISBN[strlen(ISBN)-1] = '\0'; search_ISBN(my_book_array,n,ISBN); }}
return 0; }
int parse_file(char filename[], books my_book_array[]) { FILE* infile = fopen(filename, "r"); // Attempt to open file if (infile == NULL) // Return 0 (failure) if file could not open return 0; char buffer[512]; // Create temporary string buffer variable int i = 0; // Indexer for book array int n =0; char* ptr; char *buf;
if(infile != NULL){ while (fgets(buffer, 512, infile) !=NULL) { // Loop collecting each line from the file ptr = strtok(buffer,","); // Parse string by commas and newline
if(strcmp(ptr,"N/A")) // Validate string strcpy(my_book_array[i].title,ptr);// First parse is title
ptr = strtok(NULL,", "); if(strcmp(ptr,"N/A")) strcpy(my_book_array[i].author_name,ptr);
ptr = strtok(NULL,", "); if(strcmp(ptr, "N/A")) strcpy(my_book_array[i].ISBN,ptr);
ptr = strtok(NULL,", "); if(strcmp(ptr, "N/A")) strcpy(my_book_array[i].pages,ptr);
ptr = strtok(NULL,", "); if(strcmp(ptr, "N/A")) strcpy(my_book_array[i].year_published,ptr); } while(ptr != NULL){ if(i==0){ strcpy(my_book_array[n].title,buf); } if(i==1){ strcpy(my_book_array[n].author_name,buf); } if(i==2){ strcpy(my_book_array[n].ISBN,buf); } if(i==3){ my_book_array[n].pages=atoi(buf); } if(i==4){ my_book_array[n].year_published=atoi(buf); } i++; buf = strtok(NULL,","); }} n++; fclose(infile); return 1; }
void print_book(books my_book_array[], int n){ int i; // for loop
for(i=0; i void search_title(books my_book_array[], int n, char title){ int i; for(i=0; i void search_author(books my_book_array[], int n, char author_name){ int i; for(i=0; i void search_ISBN(books my_book_array[], int n, char ISBN){ int i; for(i=0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
