The following task is to be submitted with one Header file that contains all of
ID: 3920466 • Letter: T
Question
The following task is to be submitted with one Header file that contains all of the classes and two CPP or source files that contain the function definitions for the classes and the main. 1. Take what we did in class and create a File class for easy reading and writing access. The constructor should take a string and should open a file. 2. Create a function for writing bytes to a file. The function should be a template function. With an unsigned int to indicate the index at which to write the bytes to. (This is for insertion in the middle of a file) 3. Create a function for reading bytes from a file. This function should be a template function. The function should return type t. The parameter for this function should have an unsigned int to declare the index in the file. 4. The Deconstructor should check if the file is open. If the file is open close the file.Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
//File class
class File
{
fstream file;
public:
//parameter constructor
File(char filename[])
{
//opening file
file.open(filename);
}
//default constructor
File()
{
//do nothing
}
//deconstructor
~File()//to close file..
{
if (file.fail())
{
cout << "Error!-- file not opened... ";
file.clear( );
}
else//means file opened ...then closing it
{
file.close();
}
}
//method to open file
void openFile(char filename[])
{
//opening file
file.open(filename);
}
//method to close file
void closeFile()
{
if (file.fail())
{
cout << "Error!-- file not opened... ";
file.clear( );
}
else//means file opened ...then closing it
{
file.close();
}
}
//method to write a byte to a file at index
template <class T>
void write(T b,unsigned int i)
{
//setting index position in file
file.seekp(i);
file.put(b);//inserting to file..
}
//method to read a byte to a file at index
template <class T>
void write(T &b,unsigned int i)
{
//setting index position in file
file.seekp(i);
file>>b;//reading from file....
}
};
int main()
{
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.