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<<"Error:cannot allocate memory.\n";
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 << "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;
}
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<<"Error:cannot allocate memory.\n";
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 << "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;
}