Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Re:Class template

Status
Not open for further replies.

akashvij

Technical User
Mar 11, 2002
19
US
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 &quot;MyStorage.h&quot;

int main() {

// integer type storage for 20 elements (arraySize)
MyStorage<int> storage(20);

// array size is 20
cout << &quot;storage size = &quot; << storage.getArraySize() << endl;
// no data inserted yet, so 0 is expected
cout << &quot;number of data in the storage = &quot; << storage.getDataCount() << endl;

// read data from IntegerData.txt file, and store them into the array of storage
if (!storage.readFromFile(&quot;IntegerData.txt&quot;)) {
cout << &quot;read failure: file not exist or storage size insufficient&quot; << endl;
return 1;
}

// getData() is to read one data from the storage
cout << &quot;number of data in the storage = &quot; << 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(&quot;CopyIntegerData.txt&quot;)) { // copy into out.txt
cout << &quot;write failed&quot; << endl;
return 1;
}

// sort the array in the ascending order
storage.sort();

// create another file for the sorted data
if (!storage.printIntoFile(&quot;SortedIntegerData.txt&quot;)) {
cout << &quot;write failed&quot; << endl;
return 1;
}

cout << &quot;sort done. see sorted.txt file.&quot; << 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(&quot;DoubleData.txt&quot;)) {
cout << &quot;read failure: no data file or storage size insufficient&quot; << endl;
return 1;
}

cout << &quot;number of data in the storage = &quot; << ds.getDataCount() << endl;

// sort the data, and dump the contents into a file
ds.sort();
if (!ds.printIntoFile(&quot;SortedDoubleData.txt&quot;)) {
cout << &quot;write failed&quot; << endl;
return 1;
}

return 0;
}
Note: We have two files Integerdata.txt and Doubledata.txt

i am really thankful for your help.
 
If you're using Visual c++, I suggest that you take a look at the help...

ifstream, ofstream, fopen... are some of the keywords that'll be of some use to you... But since it's a class project, I cannot be sure if your teacher ask for specific commands or not...

hope this will help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top