Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing this program in c++. Thank you very much. Class Model : For

ID: 3666213 • Letter: I

Question

I need help writing this program in c++. Thank you very much.

Class Model:

For this assignment, write a class template named Model. The purpose of the class is to be able to write text to and read text from a file. Model has the following methods:

constructor - accepts the name of a text file as it's argument.

write - Write has a single parameter. Write appends the argument passed to it to the text file whose name was provided to the constructor. If it fails to write to the file, for any reason, write should throw an exception.

read - Read accepts only an integer as an argument. The argument specifies which piece of data to read from the text file named in the constructor. For example, if the text file contains the following:

20 30 60

and the argument is 1, then read returns 30.

Since Model is a template, it should be able to be used with any data type: int, float, double, characters, strings, etc. If read fails to read from the file, for any reason, it should throw an exception.


This program makes use of STL and Exceptions in C++.

The idea is to create a class template that can read and write from a file.

An example of this would be something along these lines:

int main (){

Model<string>    m(“string.txt”);

m.write(maximum(“Hello”,”World”));

m.write(maximum(“Bernie”,”Sanders”));

cout<<m.read(0);

cout<<m.read(1);

cout<<m.read(2);

}

Output:

Hello

Bernie



Stand Alone Function Template:

In addition, write a stand alone function template that accepts two arguments and returns the larger of the two.

Demonstrate class Model and this function in a program. In my solution, I pass a couple values to the max function and then pass what it returns to the read method.


Hints:

You could make an fstream variable a private attribute of the class Model.

There should not be a statement with cin or cout anywhere in the class or the function.

Be sure to throw an exception if the text file fails to open for either reading or writing.

Review chapter 12 for advanced file io review.

sample run:

Explanation / Answer

To perform file processing in C++, header files <iostream> and <fstream> must be included in your C++ source file.

Opening a File:

A file must be opened before you can read from it or write to it. Either theofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.

Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.

Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.

You can combine two or more of these values by ORing them together. For example if you want to open a file in write mode and want to truncate it in case it already exists, following will be the syntax:

Similar way, you can open a file for reading and writing purpose as follows:

Closing a File

When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.

Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.

Writing to a File:

While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use anofstream or fstream object instead of the cout object.

Reading from a File:

You read information from a file into your program using the stream extraction operator (>>) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.

Read & Write Example:

Following is the C++ program which opens a file in reading and writing mode. After writing information inputted by the user to a file named afile.dat, the program reads information from the file and outputs it onto the screen:

When the above code is compiled and executed, it produces the following sample input and output:

Above examples make use of additional functions from cin object, like getline() function to read the line from outside and ignore() function to ignore the extra characters left by previous read statement.

File Position Pointers:

Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg ("seek get") for istream and seekp ("seek put") for ostream.

The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream,ios::cur for positioning relative to the current position in a stream or ios::endfor positioning relative to the end of a stream.

The file-position pointer is an integer value that specifies the location in the file as a number of bytes from the file's starting location. Some examples of positioning the "get" file-position pointer are:

Mode Flag Description ios::app Append mode. All output to that file to be appended to the end. ios::ate Open a file for output and move the read/write control to the end of the file. ios::in Open a file for reading. ios::out Open a file for writing. ios::trunc If the file already exists, its contents will be truncated before opening the file.