Question

In: Computer Science

A C PROGRAM *Edit/Update I do not need the file loading script, but I am not...

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).

Solutions

Expert Solution

#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.


Related Solutions

I need to update this java program to take input about each employee from a file...
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file payroll.txt Kevin Yang 60 20 Trey Adams 30 15 Rick Johnson 45 10 Cynthia Wheeler 55 11.50 Sarah Davis 24 10 Muhammad Rabish 66 12 Dale Allen 11 18 Andrew Jimenez 80 15 import java.util.Scanner; public class SalaryCalcLoop { double Rpay = 0, Opay = 0; void calPay(double hours, double rate)...
I need to update this program to follow the these requirements please and thank you :...
I need to update this program to follow the these requirements please and thank you : Do not use packages Every class but the main routine class must have a toString method . The toString method for Quadrilateral should display its fields: 4 Points. All instance variables should be declared explicitly private . Area incorrect : enter points no: 1 1 3 enter points no: 2 6 3 enter points no: 3 0 0 enter points no: 4 5 0...
I only need to update this JAVA program to be able to : 1 ) Exit...
I only need to update this JAVA program to be able to : 1 ) Exit cleanly after creating the chart 2 ) change the pie chart to a perfect circle rather than an oval 3 ) provide a separate class driver import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import java.util.Scanner; import javax.swing.JComponent; import javax.swing.JFrame; // class to store the value and color mapping of the // pie segment(slices) class Segment { double value; Color color; // constructor public...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
I need C++ program that Read an input file of text.txt one word at a time....
I need C++ program that Read an input file of text.txt one word at a time. The file should consist of about 500 words. The program should remove all punctuations,keep only words. Store the words in a built-in STL container, such as vector or map.Can someone help with any additional comments that I can understand the logic?thank you
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
I am writing a shell program in C++, to run this program I would run it...
I am writing a shell program in C++, to run this program I would run it in terminal like the following: ./a.out "command1" "command2" using the execv function how to execute command 1 and 2 if they are stored in argv[1] and argv[2] of the main function?
Here is the problem I am working on. I need to write a py program that:...
Here is the problem I am working on. I need to write a py program that: Calculates a Cartesian Product Output, A x B of 2 lists. I want to limit each list to 5 numbers. It is kind of working, but I can't help think that there is a simpler way to express this problem. Please advise... Here is my code: def CartesianProductOutput(A,B):     lst = []     for x in A:         t = []         t.append(x)         for y in B:             temp...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT