Lab (7) - More on I/O Streams

In this lab we will:
Learn to use input/output files to read/write data
Learn the array implementation of file names
Learn to create an output file, then use it as input in the same program
Use the End Of File flag (EOF) to read all the input data from a file


In this part you will write a C++ code that reads an input file, performs some calculations, then generates an output file. This code must be designed based on the structure chart given on Figure (1). This lab is closely related to Lab (6).

PART (0): Create the first input file (input1.dat)

The first input file contains the following data: The first columns, item_no, is an integer representing the item number, the second column, count, is an integer that represents number of items, and the last column, item_price, is a real value representing the price of that item in $. Here is our first input file, copy the data below **** line to a file called input1.dat.

item_no count     item_price
1              4          12.04
2              3          23.65
3              1          46.90
4              3          12.63
5              8          1.23
6              2          54.21
7              1          10.04
8              3          23.00
9              2          6.75
10            6          1.54

To copy the file use pico to open a blank file (% pico input1.dat) and then cut and paste the above data into that file, save and exit the file.

PART (1): Writing the Code

In this part we compute the total cost of each item. The total cost of each item is:

item_tot = count * item_price.

The item_tot, will be written into the output file, output1.dat, in the following format (Your output should be numbers only, i.e., everything below *** line):

item_no             item_tot
1                     48.16

… …

Here is the structure chart for this part.

Figure (1) – Structure chart for part (1)

As you can see the streams in_str1and out_str1 corresponding respectively to the in_file1 and out_file1 files are defined in the main program. This program consists of four functions: Get_files, Read_data, Get_item_total, and Write_date. Here is the description of each function:

Read_data: This function will read one line of data at the time from the input file (in_file1.dat) through the in_str1 stream and will return the values on that line, i.e., item_no, count, item_price.

Get_item_total: This function will compute the cost of each item using the given formula (item_tot = count * item_price) and will return the total cost of that item.

Write_data: This function will write to the output file (out_file1.dat) through the out_str1, the item_no and the item_tot.

Get_files: This function will return the names of the input and output files. Note that the variables (arrays) passed to the function and the name are successfully returned. The way we have the function declared. Although we haven’t used & before in_file1 and before out_file1, since these are arrays it is the same as call them by reference. We will discuss this in later part of this course. Since you haven’t used the array implementation to pass the file name to other functions, we list the Get_files function below. Whe you call this function you will use:

char in_file1[15], out_file1[15]; // declare two variables to hold the file names


Get_files(in_file1, out_file1); // get the file names


// This function will get two names for the input and output files and will return
// the two names to the main program.
void Get_files(char in_file1[], char out_file1[])
    cout << "Enter the input file \n";
    cin >> in_file1; //Enter the file name for your input
    cout << "Enter the output file \n";
    cin >> out_file1; // Enter the file name for your output

Homework (Due Nov. 1, 2000)

Now you have the output file that contains the total price of each item, out_file1.dat. Your homework is to modify your code (actually you will add to it) so that this time it reads from the out_file1.dat file and computes the total price plus tax of each item and then the total price of the whole purchase. But we will tax the items based on the item_tot. Here is the scheme for calculating the tax:

If the total price of the item (item_tot) is less than $10, apply 2% tax,

If the total price of the item is larger than or equal to 10 but smaller than $100, apply 5% tax, and

If the total price of the item is larger than $100 apply 8% tax.

Note that the total prices of items are saved in the output file of the first part (out_file1.dat) as item_tot.

We will read the item_no and item_tot from the out_file1.dat file. Then, we will use the above scheme to find the tax, we will apply the tax, and then we will write the new output file, out_file2.dat, that contains:

This file contains the itemized details and the total cost of your purchase.

item_no     item_tot_and_tax
    …                ...

The total cost of your purchase is : ?????

Here are my suggestions.

Make sure that you have closed the two files (in_file1.dat and out_file1.dat) after you finish writing the out_file1.dat file. Now you can open the out_file1.dat file as input and use the in_str1 stream to connect to it. Note: by closing the in_file1.dat, you have freed the in_str1 stream.

Write a new function, Read_Data2, that reads the new data from the out_file1.dat file.

Write a new function that takes the item_tot and returns item_tot*tax. This function will apply proper tax based on the given scheme.

You can compute the total in the main by adding these returned values.

You can use the Write_data to write the new values in the new output file, out_file2.dat.