I need help writing this program in c++. Thank you very much. Class Model: For t
ID: 3666215 • 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. 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.
This is what I have done so far.
template <class Model>
class read_write
{
private:
fstream name;
public:
read_write(Model n)
{
name=n;
}
void writ(Model w)
{
name = w;
ofstream rit_in;
rit_in.open(w);
rit_in << "Hello wourld" << endl;
rit_in.close();
}
Model Read();
};
Explanation / Answer
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <stdexcept>
using namespace std;
template <class T>
class Model {
private:
ifstream ifs;
ofstream ofs; // elements
public:
Model(string file_name)
{
ifs.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
ofs.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
try{
ofs.open(file_name.c_str(), ios::app);
}
catch(exception const& ex)
{
cout<<"not able to open file for write"<<endl;
}
try
{
ifs.open(file_name.c_str());
}
catch(exception const& ex)
{
cout<<"not able to open file for read"<<endl;
}
}
void write(T ele)
{
try
{
ofs<<ele<<" ";
}
catch(exception const& ex)
{
cout<<"not able to write to file"<<endl;
}
}
T read(int n)
{
try
{
T res;
for(int i=0;i<n;i++)
{
ifs>>res;
}
return res;
}
catch(exception const& ex)
{
cout<<"not able to read the given index element"<<endl;
}
}
};
template <typename T>
T larger (T a, T b)
{
return a < b ? b:a;
}
int main()
{
float n;
int i;
Model<float> m("test.txt");
do
{
cout<<"enter the value you want to write to file, enter 0 to stop writing"<<endl;
cin>>n;
m.write(n);
}while(n!=0);
cout<<"enter the index of the value you want to read"<<endl;
cin>>i;
n=m.read(i);
cout<<n<<endl;
cout<<"larger of 2,3 is "<<larger(2,3)<<endl;
cout<<"larger of 23.5,56.7 is "<<larger(23.5,56.7)<<endl;
}
output:
3.2
larger of 2,3 is 3
larger of 23.5,56.7 is 56.7