In: Computer Science
A C PROGRAM
*Edit/Update I do not need the file loading script, but I am not against it being included in the answer*
I must read off of an excel file (comma separated) to input five different things for a book, all comma separated, there are 360 books in the file. The book title, author, ISBN number, number of pages, and finally the year it was published. Now some of the ISBN or Pg numbers may be missing and will be read as N/A in the file.
a structure named book is with the following inputs to be used
a. char title[255];
b. char author_name[50];
c. char ISBN[10];
d. int pages;
e. int year_published;
Create a function called print_book ( ) that takes a book as a parameter. The function should neatly print the contents of that book’s structure to the terminal window. Remember: some records might have null parameters, make sure to handle that in your output.
Finally
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).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STR_LEN 2048
#define MAX_BOOKS 10000
struct book{
char title[256];
char author_name[256];
char ISBN[10];
int pages;
int year_published;
};
struct book books[MAX_BOOKS];
int readBookFile()
{
/* FileStream for the Library File */
FILE *File;
/* alocating the space for the reading buffer */
char *buf = malloc(MAX_STR_LEN);
char *temp;
/* See if there is enough space in memory for all the books
*/
if (buf == NULL) {
printf ("No memory\n");
return 1;
}
/*Reading the file and making sure that whe are able to read it,
make sure to change the name of the file for your use*/
if ( ( File = fopen( "input.txt", "r" ) ) == NULL )
{
printf( "File could not be opened.\n" );
}
/*using fgets to read a file line and separating each book by
comma*/
int i = 0;
while (fgets(buf, 255, File) != NULL)
{
if ((strlen(buf)>0) && (buf[strlen (buf) - 1] ==
'\n'))
buf[strlen (buf) - 1] = '\0';
temp = strtok(buf, ",");
strcpy( books[i].title, temp);
temp = strtok(NULL, ",");
strcpy( books[i].author_name, temp);
temp = strtok(NULL, ",");
strcpy( books[i].ISBN, temp);
temp = strtok(NULL, ",");
books[i].pages = atoi(temp);
temp = strtok(NULL, ",");
books[i].year_published = atoi(temp);
/*printing each reading book for debugging porpuses*/
/*printf("ID: %d, %s, %s, %s, %d, %d\n",i ,books[i].title ,
books[i].author_name, books[i].ISBN , books[i].pages,
books[i].year_published);*/
i++;
}
/*make sure to always close the file*/
fclose(File);
return i;
}
/*function to print book*/
void print_Book(int book_number) {
printf("%d, %s, %s, %s, %d,
%d\n",book_number,books[book_number].title ,
books[book_number].author_name, books[book_number].ISBN ,
books[book_number].pages, books[book_number].year_published);
}
/*function to search for a book*/
void search_Book(char book_to_search[256], int begin, int
end)
{
int i;
/*i = sizeof(books) / sizeof(books[0]);
printf ("%i \n", i);*/
for (i = begin; i < end; i++)
{
if (strcmp(books[i].title, book_to_search) == 0) {
print_Book(i);
}
}
}
int main(int argc, char **argv)
{
/*call the reading function*/
int size = readBookFile();
/*searching and printing a book*/
search_Book("title2",0,size);
return 0;
}
COMMENT DOWN FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.