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!

storing a file?

Status
Not open for further replies.

akashvij

Technical User
Mar 11, 2002
19
US
i am working with two files integerdata.txt and doubledata.txt.i am haveing trouble reading the data and storing it in array and then dumping it again in a file named 'filename'. i am really thankful to you for helping me out.

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

};


template<class T>
MyStorage<T>::MyStorage(int s)
{
arraysize = s;
arrayPtr = new T;
if (arrayPtr == 0)
memError();
for (int count=0;count<arraysize;count++)
*(arrayPtr+count) = 0;
}

template <class T>
void MyStorage<T>::memError(void)
{
cout<<&quot;Error:cannot allocate memory.\n&quot;;
exit(0);
}

template <class T>
T MyStorage<T>::getArraySize ()
{ return ArraySize;}

template<class T>
T MyStorage<T>::getDataCount ()
{ return datacount;}


# endif




Main function is give as follows.
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;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top