Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implemented in C++. How would I read all data in the inventory from a text file and store the data in the inventory database? I

Implemented in C++.

How would I read all data in the inventory from a text file and store the data in the inventory database?

I have a Book_Inventory & BookRecord class.

Given this text file:

# Any line beginning with a pound sign shall be ignored as a comment line # First non-comment line will give the number of books in the file # Remaining lines will give the book ID, title, classification, cost # and number in stock. Each shall be on a separate line. 6 2345 More Fun With C++ 613 29.95 15 1234 Having Fun With C 613 29.95 10 4567 Oh Say Can You C 316 19.95 20 3456 Geeks Guide to C 316 24.95 15 9876 C Spot Program 613 15.95 12 5678 C++ Code Secrets 316 24.95 12

And given these two functions.

#1

bool Book_Inventory::readInventory(char *filename)

{

char line[128];

int numBooks;

long IdNum;

char title[12];

int classification;

double cost;

int numInStock;

string dare;

m_InFile.open(filename, ifstream::in);

if(!m_InFile.is_open())

{

// m_InFile.is_open() returns false if the file could not be found or

// if for some other reason the open failed.

cout << "Unable to open file" << filename << " Program terminating... ";

return false;

}

// Read number of books

getNextLine(line, 128);

numBooks = atoi(line);

for(int i=0; i

{

// ------------------------------------------ // Your code to read the inventory goes here // ------------------------------------------

}

return true;

}

#2

bool Book_Inventory::getNextLine(char *line, int lineLen)

{

int done = false;

while(!done)

{

m_InFile.getline(line, lineLen);

if(m_InFile.good()) // If a line was successfully read

{

if(strlen(line) == 0) // Skip any blank lines

continue;

else if(line[0] == '#') // Skip any comment lines

continue;

else return true; // Got a valid data line so return with this line

}

else // No valid line read, meaning we reached the end of file

{

strcpy(line, ""); // Copy empty string into line as sentinal value

return false;

}

} // end while

return false; // Cannot reach this point but include a return to avoid compiler warning

// that not all paths return a value.

}

The For loop inside reafInventory is the main area in which code is needed.

Thanks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions