but i am having problem how can i code for this member function
bool readfromfile(char *filename)
// code.
//we are to read data of type t from the file named 'filename'and store the data into the array.returns true if reading was successful,false otherwise.
here is actual assignment
template <class T>
class MyStorage {
private:
T *arrayPtr; // pointer to an array
int arraySize; // size of the array
int dataCount; // actual number of data stored in the array
public:
MyStorage(int size);
int getArraySize();
int getDataCount();
bool readFromFile(char *fileName);
bool printIntoFile(char *fileName);
T getData(int index);
void sort(); // sort in the ascending order
};
// Give member function definitions below. DO NOT give as inline member function.
Description of member functions
MyStorage(int size) à Constructor. An array of T type with ‘size’ elements is allocated for the pointer ‘arrayPtr’
int getArraySize() à returns ‘arraySize’; size of the array allocated.
int getDataCount() à returns ‘dataCount’; actual number of data stored in the array.
bool readFromFile(char *filename) à read data of type T from the file named ‘filename’, and store the data into the array. dataCount is updated accordingly. Returns true if reading was successful, false otherwise.
bool printIntoFile(char *filename) à dump the contents of the array into a file named ‘filename’. Returns true if writing was successful, false otherwise.
T getData(int index) à Returns the data at the given index of the array.
void sort() à Sorts the data in the array in the ascending order. Refer to page 562 for bubble sort. Modify the code such that the sort works for any type (T) of data.
main is also given
#include <iostream>
using namespace std;
#include "MyStorage.h"
int main() {
// integer type storage for 20 elements (arraySize)
MyStorage<int> storage(20);
// array size is 20
cout << "storage size = " << storage.getArraySize() << endl;
// no data inserted yet, so 0 is expected
cout << "number of data in the storage = " << storage.getDataCount() << endl;
// read data from IntegerData.txt file, and store them into the array of storage
if (!storage.readFromFile("IntegerData.txt") {
cout << "read failure: file not exist or storage size insufficient" << endl;
return 1;
}
// getData() is to read one data from the storage
cout << "number of data in the storage = " << storage.getDataCount() << endl;
for (int i = 0; i < storage.getDataCount(); i++)
cout << storage.getData(i) << endl;
// dump the contents of the storage into a file
if (!storage.printIntoFile("CopyIntegerData.txt") { // copy into out.txt
cout << "write failed" << endl;
return 1;
}
// sort the array in the ascending order
storage.sort();
// create another file for the sorted data
if (!storage.printIntoFile("SortedIntegerData.txt") {
cout << "write failed" << endl;
return 1;
}
cout << "sort done. see sorted.txt file." << endl;
// double type storage is created for 15 elements (array size = 15)
MyStorage<double> ds(15);
// read data from DoubleData.txt, and store data into ds
if (!ds.readFromFile("DoubleData.txt") {
cout << "read failure: no data file or storage size insufficient" << endl;
return 1;
}
cout << "number of data in the storage = " << ds.getDataCount() << endl;
// sort the data, and dump the contents into a file
ds.sort();
if (!ds.printIntoFile("SortedDoubleData.txt") {
cout << "write failed" << endl;
return 1;
}
return 0;
}
Note: We have two files Integerdata.txt and Doubledata.txt
i am really thankful for your help.
bool readfromfile(char *filename)
// code.
//we are to read data of type t from the file named 'filename'and store the data into the array.returns true if reading was successful,false otherwise.
here is actual assignment
template <class T>
class MyStorage {
private:
T *arrayPtr; // pointer to an array
int arraySize; // size of the array
int dataCount; // actual number of data stored in the array
public:
MyStorage(int size);
int getArraySize();
int getDataCount();
bool readFromFile(char *fileName);
bool printIntoFile(char *fileName);
T getData(int index);
void sort(); // sort in the ascending order
};
// Give member function definitions below. DO NOT give as inline member function.
Description of member functions
MyStorage(int size) à Constructor. An array of T type with ‘size’ elements is allocated for the pointer ‘arrayPtr’
int getArraySize() à returns ‘arraySize’; size of the array allocated.
int getDataCount() à returns ‘dataCount’; actual number of data stored in the array.
bool readFromFile(char *filename) à read data of type T from the file named ‘filename’, and store the data into the array. dataCount is updated accordingly. Returns true if reading was successful, false otherwise.
bool printIntoFile(char *filename) à dump the contents of the array into a file named ‘filename’. Returns true if writing was successful, false otherwise.
T getData(int index) à Returns the data at the given index of the array.
void sort() à Sorts the data in the array in the ascending order. Refer to page 562 for bubble sort. Modify the code such that the sort works for any type (T) of data.
main is also given
#include <iostream>
using namespace std;
#include "MyStorage.h"
int main() {
// integer type storage for 20 elements (arraySize)
MyStorage<int> storage(20);
// array size is 20
cout << "storage size = " << storage.getArraySize() << endl;
// no data inserted yet, so 0 is expected
cout << "number of data in the storage = " << storage.getDataCount() << endl;
// read data from IntegerData.txt file, and store them into the array of storage
if (!storage.readFromFile("IntegerData.txt") {
cout << "read failure: file not exist or storage size insufficient" << endl;
return 1;
}
// getData() is to read one data from the storage
cout << "number of data in the storage = " << storage.getDataCount() << endl;
for (int i = 0; i < storage.getDataCount(); i++)
cout << storage.getData(i) << endl;
// dump the contents of the storage into a file
if (!storage.printIntoFile("CopyIntegerData.txt") { // copy into out.txt
cout << "write failed" << endl;
return 1;
}
// sort the array in the ascending order
storage.sort();
// create another file for the sorted data
if (!storage.printIntoFile("SortedIntegerData.txt") {
cout << "write failed" << endl;
return 1;
}
cout << "sort done. see sorted.txt file." << endl;
// double type storage is created for 15 elements (array size = 15)
MyStorage<double> ds(15);
// read data from DoubleData.txt, and store data into ds
if (!ds.readFromFile("DoubleData.txt") {
cout << "read failure: no data file or storage size insufficient" << endl;
return 1;
}
cout << "number of data in the storage = " << ds.getDataCount() << endl;
// sort the data, and dump the contents into a file
ds.sort();
if (!ds.printIntoFile("SortedDoubleData.txt") {
cout << "write failed" << endl;
return 1;
}
return 0;
}
Note: We have two files Integerdata.txt and Doubledata.txt
i am really thankful for your help.