Question
Create a program that reads data from an unformatted, tab-delimited file, products.txt , performs some calculations on that data, and writes the resulting data to
Create a program that reads data from an unformatted, tab-delimited file, products.txt, performs some calculations on that data, and writes the resulting data to another file called product_list.txt, thats formatted and uses spaces to align the columns (see specification of this file shown below).
You may use g++ or an IDE to develop this program. In either case, the files you submit for grading must meet the condition described next.
Console
Specifications
- The input file products.txt contains the product data shown above, which is not supposed to be modified. Note that the total number of lines of data in this file could be changed later by adding more or deleting some of them, meaning your program cannot assume it contains fixed number of lines of data.
- The program should read the data from the products.txt file which should be placed in the directory where the program is loacted. It calculates the discount amount and list price from this data, and writes the result to a text file named product_list.txt in the same directory where the program is located. This data should be formatted and aligned using spaces as shown above. See p.168~9 for how to write formatted data to a text file.
- The program uses a class called ProductFile to handle file I/O operations. This class must be implemented in two files, productFile.h (for all members declaration) and productFile.cpp (for all members definition).
- ProductFile class has a private string member 'where' to store a file name.
- ProductFile class provides three and only three public functions as its interface:
- a constructor to set a file name to its 'where' member.
- readFile(), whose main duty is to create an input file stream object to open the text file indicated by the 'where' member and return the stream object. If the file opens, it displays "Reading product data from ...... file." Otherwise, it displays "Could not find input file named ......." It is noted that this function does not close the stream object because it is going to be used by the next function.
- writeFile(), which accepts an input file stream object and an out file name. It displays "Writing product data to ...... file" to indicate it starts working on the file. The first task is to use the given stream object to read all three pieces of data of each product, including product code, list price, and discount %, then calculate discount amount and discounted price. Its second task is to write a title and header followed by five data each product to a text file whose name is specified as this function's 2nd parameter. Be sure this function closes both stream objects at the end.
For the purpose of this assignment, writeFile() should use the eof() function of the input file stream to determine if there are more data in the stream to extract. It must also use the fail(), clear() and ignore() functions to fix the stream and try again when the extraction fails because of bad data.
**See p.167 for a good pattern and code example of using these stream functions.
**You may want to prepare your own file to test this part of your program.
- The program's main() is implemented in prog4.cpp file. This should be a very simple and small function because it only prints "Product List" when the program starts and "Bye!" when it finishes. In between them, it makes a ProductFile object and tells it the data file name is "products.txt". Then, it makes a file stream object and asks it to do readFile(). Finally, before prints "Bye!", it lets the stream object perform writeFile().
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started