Вы находитесь на странице: 1из 8

COP3014

Assignment 7

Programming I

Assignment 7
Contents
Contents .........................................................................................................................................................................1
Introduction ...................................................................................................................................................................1
Submission Format ........................................................................................................................................................1
Template ........................................................................................................................................................................1
Walkthrough ..................................................................................................................................................................2
Function Prototypes ...................................................................................................................................................2
Sample Operation ......................................................................................................................................................2
Notes and Hints .............................................................................................................................................................7

Introduction
This assignment will introduce you to the capabilities of streams. We are mainly concerned with file streams but
there are a couple of others worth noting as well. Strangely enough, you already know everything you need to know
about them. You have been using them this entire semester since the very first assignment. Do you recall reading
about cin and cout streams? Behind the scenes, cin and cout take care of a lot complicated things. For example,
when you send cout a string (i.e. cout << My string!) its pretty obvious what needs to happen. Cout formats your
string into its ASCII representation and makes it appear in the console. What about integers and doubles, though?
Under the covers, cout converts your digits to ASCII and prints them as well. In this assignment, instead of sending
all of our input to a console input or output (cin and cout, respectively), we will be sending it to either a string
stream or a file stream. The difference now is that the information you send to the stream will show up either
inside of a string or a file.

Submission Format
1.
2.
3.

You should place your code in one (1) file named StreamOperations.cpp
Zip up the file and title the zip with the following format: FSUID, underscore, assign7 (e.g. John F. Doe
might be JFD13_assign7.zip)
Submit the zip file.

Template

COP3014

Assignment 7

Programming I

A template file named a7template.cpp is provided on the BlackBoard website. It provides the function prototypes for
you. Your job is to create and fill the functions with the necessary logic.
Another file named cars.txt is provided for you to test with. The format of the file is as follows:
[year] [make] [model] [price] [mileage] [category]
You will need to read this file in and process it as described below.

Walkthrough
Function Prototypes
Whenever a function had enough parameters to make it wrap, I wrote them in column format.
void
void
void
void
void

GetInputFileStream(std::ifstream * fin, std::string filename);


GetOutputFileStream(std::ofstream * fout, std::string filename);
SetGetPointer(std::istream & fin, int location);
SetPutPointer(std::ostream & fout, int location);
AnalyzeFile(std::istream & fin,
int & numUsed,
int & numNew,
double & newTotalPrice,
double & newTotalMileage,
double & usedTotalPrice,
double & usedTotalMileage);
void PrintLine(std::ostream & sout, std::string s);
void PrintNew(std::istream & fin, std::ostream & fout);
void PrintUsed(std::istream & fin, std::ostream & fout);
void PrintStatistics(std::ostream & fout,
int numUsed,
int numNew,
double newTotalPrice,
double newTotalMileage,
double usedTotalPrice,
double usedTotalMileage);
std::string FormatCarInfo(std::string year,
std::string make,
std::string model,
double price,
double mileage);

Sample Operation
In this section, pay very close attention to the output. When specified, yours should match exactly. Also, this section
will include hints on how to get the appropriate functionality to work.
bool GetInputFileStream(std::ifstream * fin, std::string filename);
o fin: A pointer to an input file stream object.
o filename: The name of the file you want to open
o Returns: true or false, depending on whether the file opened successfully or not.

The caller will supply a pointer to an input file stream object. You will use this object to open an input file that has
the name filename. If the file doesnt exist, alert the user by returning false.

COP3014

Assignment 7

Sample Usage

Sample Output

std::ifstream fin; // 'f'ile in - fin


std::string filename = "fake.txt";
bool isOpen = GetInputFileStream(&fin, filename);
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;
std::cout << std::endl;
filename = "real.txt";
isOpen = GetInputFileStream(&fin, filename);
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;
std::cout << std::endl;
fin.close();

Opening file fake.txt


fake.txt open: false

Programming I

Opening file real.txt


real.txt open: true

void GetOutputFileStream(std::ofstream * fout, std::string filename);


o
fout: A pointer to an output file stream object
o
filename: The name of the file that you want to open
o
Returns: Nothing.

The caller will supply a pointer to an output file stream object. You will use this object to open an output file that
has the name filename. For this assignment we will assume that output files always open successfully. Once the
code below runs you should have a file with the given name and the text that you inserted into the stream. You can
then open it using notepad (or whatever) and see the output as shown below on the right.
Sample Usage

Sample Output

std::ofstream fout; // 'f'ile out - fout


std::string filename = "newFile.txt";
GetOutputFileStream(&fout, filename);
fout << "This is my new file!\n";
fout << "This is on a new line!";
fout.close();

void SetGetPointer(std::istream & sin, int location);


o
sin: An input stream object
o
location: A location, with respect to the BEGINNING of the stream, to which you want to
move the pointer to.
o
Returns: nothing

When you are reading from the stream, each time you read a character the pointer is incremented by one. That way
you always get the next character. What if you want to read the same file twice? Well, you need to reset the get
pointer back to 0. In this example, pretend that the file numbers.txt exists and that it has inside it the numbers 1, 2, 3,
4, and 5 in order with a space in-between. You notice that the count resets back to 1 when the get pointer is put back
at the 0 location of the file.
Sample Usage

Sample Output

std::ifstream fin; // 'f'ile in - fin


std::string filename = "numbers.txt";
bool isOpen = GetInputFileStream(&fin, filename);
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;
int number = 0;
fin >> number;
std::cout << "Read: " << number << std::endl;

Opening file numbers.txt


numbers.txt open: true
Read: 1
Read: 2
Read: 1
Read: 2
Read: 3
Read: 4

COP3014

Assignment 7

Sample Usage

Sample Output

fin >> number;


std::cout << "Read: " << number << std::endl;
SetGetPointer(fin, 0);
while (fin >> number)
{
std::cout << "Read: " << number << std::endl;
}
fin.close();

Read: 5

Programming I

void SetPutPointer(std::ostream & fout, int location);


o
fout: An output file stream object.
o
location: The location that you want to set the put pointer to.
o
Returns: nothing

When you are writing to a file you can move the put pointer around. It would allow you to put text in a file, send
the pointer back to 0, and overwrite whatever information was already there. In the example below, I put all of the
alphabet in lowercase into the file. I then put the pointer a 0 and rewrite half the alphabet in uppercase.
Sample Usage

Sample Output

std::ofstream fout; // 'f'ile out - fout


std::string filename = "setPutExample.txt";
GetOutputFileStream(&fout, filename);
fout << "abcdefghijklmnopqrstuvwxyz";
SetPutPointer(fout, 0);
fout << "ABCDEFGHIJKLMN";
fout.close();

void PrintStatistics(std::ostream & sout, int numUsed, int numNew, double newTotalPrice, double
newTotalMileage, double usedTotalPrice, double usedTotalMileage);
o
sout: An output file stream object
o
numUsed: the number of used cars
o
numNew: the number of new cars
o
newTotalPrice: the total price of all new cars
o
newTotalMileage: the total mileage of all new cars
o
usedTotalPrice: the total price of all used cars
o
usedTotalMileage: the total mileage of all used cars
o
returns: nothing

In this assignment you will be analyzing the statistics of a file comprising a list of new and used cars shipped to
Bobs Car Lot. After you have analyzed the file you will need to print the results. Follow this output exactly. I
highly recommend the use of setw (iomanip). Notice that each column is as wide as the header (Category, Number,
Total Price, etc.) plus 1 (except the first column has no space at the beginning). The vertical line after each category
is called a pipe. It is usually on the same key as the backslash right above the enter key. The beauty of this function
is that it takes an ostream as a parameter. What is an ostream? Well, it can be a console output stream (cout), a file
output stream, or even a string stream (well discuss later).
Sample Usage
// These are example values
// You should get the real ones from the file
double newTotalPrice = 33333;
double newTotalMileage = 44444;
double usedTotalPrice = 22222;
double usedTotalMileage = 99999;
int numUsed = 2;
int numNew = 3;
std::ofstream fout; // 'f'ile out - fout

Sample Output
// First it prints to the console

COP3014

Assignment 7

Sample Usage
std::string filename = "stats.txt";
GetOutputFileStream(&fout, filename);
// Print to screen
PrintStatistics(std::cout,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);
// Print to file
PrintStatistics(std::cout,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);

Programming I

Sample Output

// Then it creates a file and prints the same


// info.

void AnalyzeFile(std::istream & sin,int & numUsed, int & numNew, double & newTotalPrice, double &
newTotalMileage, double & usedTotalPrice, double & usedTotalMileage);
o
sin: An input file stream object
o
numUsed: the number of used cars in the file
o
numNew: the number of new cars in the file
o
newTotalPrice: the aggregate price of all new cars
o
newTotalMileage: the aggregate mileage of all new cars
o
usedTotalPrice: the aggregate price of all used cars
o
usedTotalMileage: the aggregate mileage of all used cars

Here is a sample input text file:

This function should read the entire file and compile the statistics. Since each of the variables are reference
variables, if you make changes here they should affect the variables used to call the function. After reading in the
entire file you should be able to print the statistics.
Sample Usage

Sample Output

double newTotalPrice = 0;
double newTotalMileage = 0;
double usedTotalPrice = 0;
double usedTotalMileage = 0;
int numUsed = 0;
int numNew = 0;
std::ifstream fin; // 'f'ile in - fin
std::string filename = "cars.txt";
bool isOpen = GetInputFileStream(&fin, filename);
std::ofstream fout;
GetOutputFileStream(&fout, "output.txt");
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;

Opening file cars.txt


cars.txt open: true
Category| Number| Total Price| Total Mileage|
New|
1|
33000|
250|
Used|
2|
7000|
254000|

COP3014

Assignment 7

Sample Usage

Programming I

Sample Output

AnalyzeFile(fin,
numUsed,
numNew,
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);
PrintStatistics(fout,
numUsed,
numNew,]
newTotalPrice,
newTotalMileage,
usedTotalPrice,
usedTotalMileage);

void PrintLine(std::ostream & sout, std::string s);


o
sout: an output stream
o
s: a string to write to the stream
o
returns: nothing

This function simply prints a line to whatever output stream is given. The output stream might be cout, an output file
stream, or a string stream.
Sample Usage

Sample Output

std::stringstream ss;
std::ofstream fout; // 'f'ile out - fout
std::string filename = "printLineExample.txt";
GetOutputFileStream(&fout, filename);
std::string s = "My output string!";
PrintLine(std::cout, s);
PrintLine(ss, s);
std::cout << "SS: " << ss.str();
PrintLine(fout, s);
fout.close();

My output string!
SS: My output string!

// This file will be created also

void PrintNew(std::istream & sin, std::ostream & sout);


o
sin: an input stream
o
sout: an output stream
o
Returns: nothing

This function scans the stream for all new and used cars and only prints the new cars to the sout stream. The sout
stream might be cout, an output file stream, or a string stream.
Sample Usage

Sample Output

std::ifstream fin; // 'f'ile in - fin


std::string filename = "cars.txt";
bool isOpen = GetInputFileStream(&fin, filename);
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;
PrintNew(fin, std::cout);
std::stringstream ssout;
PrintNew(fin, ssout);
// use ssout.str() to turn a stringstream
// into a std::string
PrintLine(std::cout, "SS: " + ssout.str());

Opening file cars.txt


cars.txt open: true
2015 Jeep Wrangler 33000 250
SS: 2015 Jeep Wrangler 33000 250

COP3014

Assignment 7

Programming I

void PrintUsed(std::istream & sin, std::ostream & sout);


o
sin: an input stream
o
sout: an output stream
o
returns: nothing

This function is the same as PrintNew except it only prints the used cars.
Sample Usage

Sample Output

std::ifstream fin; // 'f'ile in - fin


std::string filename = "cars.txt";
bool isOpen = GetInputFileStream(&fin, filename);
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;
PrintUsed(fin, std::cout);
std::stringstream ssout;
PrintUsed(fin, ssout);
// use ssout.str() to turn a stringstream
// into a std::string
PrintLine(std::cout, "SS: " + ssout.str());

Opening file cars.txt


cars.txt open: true
1999 Ford Ranger 3000 156000
2000 Mazda Miata 4000 98000
SS: 1999 Ford Ranger 3000 156000
2000 Mazda Miata 4000 98000

std::string FormatCarInfo(std::string year, std::string make, std::string model, double price,


double mileage);
o
year: the year of the car
o
make: the make of the car
o
model: the model of the car
o
price: the price of the car
o
mileage:A the mileage of the car
o
returns: a string containing the formatted information

Youve already read the file searching for new and used cars to print. Now, you just need to format EACH line of
the file and return it as a string. The string will just be each value separated by a space.
Sample Usage

Sample Output

std::string year;
std::string make;
std::string model;
double price;
double mileage;
std::string category;
std::ifstream fin; // 'f'ile in - fin
std::string filename = "cars.txt";
bool isOpen = GetInputFileStream(&fin, filename);
std::cout << filename << " open: ";
std::cout << std::boolalpha << isOpen << std::endl;
while (fin >> year &&
fin >> make &&
fin >> model &&
fin >> price &&
fin >> mileage &&
fin >> category)
{
std::string s = FormatCarInfo(year, make, model, price, mileage);
PrintLine(std::cout, s);
}

Opening file cars.txt


cars.txt open: true
1999 Ford Ranger 3000 156000
2000 Mazda Miata 4000 98000
2015 Jeep Wrangler 33000 250

Notes and Hints


1.

Use the main() function provided in the template to develop any tests you think are necessary.
7

COP3014

Assignment 7

Programming I

2.

The main() function that you implement will be deleted in the assessment. A new main() function will be
inserted that tests your functions.
3. Your code needs comments! You will be deducted significant points depending on how shy your code is on
comments.
4. http://www.cplusplus.com/reference/fstream/ofstream/?kw=ofstream
5. http://www.cplusplus.com/reference/fstream/ifstream/?kw=ifstream
6. http://www.cplusplus.com/reference/fstream/ifstream/is_open/
7. http://www.cplusplus.com/reference/istream/istream/seekg/
8. http://www.cplusplus.com/reference/ostream/ostream/seekp/?kw=seekp
9. http://www.cplusplus.com/reference/ios/boolalpha/
10. http://www.cplusplus.com/reference/sstream/stringstream/?kw=stringstream

Вам также может понравиться