home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates sequential binary file I/O
- */
-
- #include <iostream.h>
- #include <fstream.h>
-
- const unsigned MIN_SIZE = 10;
- const double BAD_VALUE = -1.0e+30;
- enum boolean { false, true };
-
- class Array
- {
- protected:
- double *dataPtr;
- unsigned size;
- double badIndex;
-
- public:
- Array(unsigned Size = MIN_SIZE);
- ~Array()
- { delete [] dataPtr; }
- unsigned getSize() const { return size; }
- double& operator [](unsigned index)
- { return (index < size) ? *(dataPtr + index) : badIndex; }
- boolean writeElem(fstream& os, unsigned index);
- boolean readElem(fstream& is, unsigned index);
- boolean writeArray(const char* filename);
- boolean readArray(const char* filename);
- };
-
- Array::Array(unsigned Size)
- {
- size = (Size < MIN_SIZE) ? MIN_SIZE : Size;
- badIndex = BAD_VALUE;
- dataPtr = new double[size];
- }
-
- boolean Array::writeElem(fstream& os, unsigned index)
- {
- if (index < size) {
- os.write((unsigned char*)(dataPtr + index), sizeof(double));
- return (os.good()) ? true : false;
- }
- else
- return false;
- }
-
- boolean Array::readElem(fstream& is, unsigned index)
- {
- if (index < size) {
- is.read((unsigned char*)(dataPtr + index), sizeof(double));
- return (is.good()) ? true : false;
- }
- else
- return false;
- }
-
- boolean Array::writeArray(const char* filename)
- {
- fstream f(filename, ios::out | ios::binary);
-
- if (f.fail())
- return false;
- f.write((unsigned char*) &size, sizeof(size));
- f.write((unsigned char*)dataPtr, size * sizeof(double));
- f.close();
- return (f.good()) ? true : false;
- }
-
- boolean Array::readArray(const char* filename)
- {
- fstream f(filename, ios::in | ios::binary);
- unsigned sz;
-
- if (f.fail())
- return false;
- f.read((unsigned char*) &sz, sizeof(sz));
- // need to expand the array
- if (sz != size) {
- delete [] dataPtr;
- dataPtr = new double[sz];
- size = sz;
- }
- f.read((unsigned char*)dataPtr, size * sizeof(double));
- f.close();
- return (f.good()) ? true : false;
- }
-
- main()
- {
- const unsigned SIZE1 = 10;
- const unsigned SIZE2 = 20;
- char* filename1 = "array1.dat";
- char* filename2 = "array3.dat";
- Array arr1(SIZE1), arr2(SIZE1), arr3(SIZE2);
- fstream f(filename1, ios::out | ios::binary);
-
- // assign values to array arr1
- for (unsigned i = 0; i < arr1.getSize(); i++)
- arr1[i] = 10 * i;
-
- // assign values to array arr3
- for (i = 0; i < SIZE2; i++)
- arr3[i] = i;
-
- cout << "Array arr1 has the following values:\n";
- for (i = 0; i < arr1.getSize(); i++)
- cout << arr1[i] << " ";
- cout << "\n\n";
-
- // write elements of array arr1 to the stream
- for (i = 0; i < arr1.getSize(); i++)
- arr1.writeElem(f, i);
- f.close();
-
- // reopen the stream for input
- f.open(filename1, ios::in | ios::binary);
-
- for (i = 0; i < arr1.getSize(); i++)
- arr2.readElem(f, i);
- f.close();
-
- // display the elements of array arr2
- cout << "Array arr2 has the following values:\n";
- for (i = 0; i < arr2.getSize(); i++)
- cout << arr2[i] << " ";
- cout << "\n\n";
-
- // display the elements of array arr3
- cout << "Array arr3 has the following values:\n";
- for (i = 0; i < arr3.getSize(); i++)
- cout << arr3[i] << " ";
- cout << "\n\n";
-
- // write the array arr3 to file ARRAY3.DAT
- arr3.writeArray(filename2);
- // read the array arr1 from file ARRAY3.DAT
- arr1.readArray(filename2);
-
- // display the elements of array arr1
- cout << "Array arr1 now has the following values:\n";
- for (i = 0; i < arr1.getSize(); i++)
- cout << arr1[i] << " ";
- cout << "\n\n";
- return 0;
- }