home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / io2.cpp < prev    next >
C/C++ Source or Header  |  1993-04-02  |  4KB  |  147 lines

  1. /*
  2.    C++ program that demonstrates sequential binary file I/O
  3. */
  4.  
  5. #include <iostream.h>
  6. #include <fstream.h>
  7.  
  8. const unsigned MIN_SIZE = 10;
  9. const double BAD_VALUE = -1.0e+30;
  10. enum boolean { false, true };
  11.  
  12. class Array
  13. {
  14.    protected:
  15.      double *dataPtr;
  16.      unsigned size;
  17.      double badIndex;
  18.  
  19.    public:
  20.      Array(unsigned Size = MIN_SIZE);
  21.      ~Array()
  22.        { delete [] dataPtr; }
  23.      unsigned getSize() const { return size; }
  24.      double& operator [](unsigned index)
  25.      { return (index < size) ? *(dataPtr + index) : badIndex; }
  26.      boolean writeElem(fstream& os, unsigned index);
  27.      boolean readElem(fstream& is, unsigned index);
  28.      boolean writeArray(const char* filename);
  29.      boolean readArray(const char* filename);
  30. };
  31.  
  32. Array::Array(unsigned Size)
  33. {
  34.   size = (Size < MIN_SIZE) ? MIN_SIZE : Size;
  35.   badIndex = BAD_VALUE;
  36.   dataPtr = new double[size];
  37. }
  38.  
  39. boolean Array::writeElem(fstream& os, unsigned index)
  40. {
  41.    if (index < size) {
  42.      os.write((unsigned char*)(dataPtr + index), sizeof(double));
  43.      return (os.good()) ? true : false;
  44.    }
  45.    else
  46.      return false;
  47. }
  48.  
  49. boolean Array::readElem(fstream& is, unsigned index)
  50. {
  51.    if (index < size) {
  52.      is.read((unsigned char*)(dataPtr + index), sizeof(double));
  53.      return (is.good()) ? true : false;
  54.    }
  55.    else
  56.      return false;
  57. }
  58.  
  59. boolean Array::writeArray(const char* filename)
  60. {
  61.    fstream f(filename, ios::out | ios::binary);
  62.  
  63.    if (f.fail())
  64.      return false;
  65.    f.write((unsigned char*) &size, sizeof(size));
  66.    f.write((unsigned char*)dataPtr, size * sizeof(double));
  67.    f.close();
  68.    return (f.good()) ? true : false;
  69. }
  70.  
  71. boolean Array::readArray(const char* filename)
  72. {
  73.    fstream f(filename, ios::in | ios::binary);
  74.    unsigned sz;
  75.  
  76.    if (f.fail())
  77.      return false;
  78.    f.read((unsigned char*) &sz, sizeof(sz));
  79.    // need to expand the array
  80.    if (sz != size) {
  81.      delete [] dataPtr;
  82.      dataPtr = new double[sz];
  83.      size = sz;
  84.    }
  85.    f.read((unsigned char*)dataPtr, size * sizeof(double));
  86.    f.close();
  87.    return (f.good()) ? true : false;
  88. }
  89.  
  90. main()
  91. {
  92.   const unsigned SIZE1 = 10;
  93.   const unsigned SIZE2 = 20;
  94.   char* filename1 = "array1.dat";
  95.   char* filename2 = "array3.dat";
  96.   Array arr1(SIZE1), arr2(SIZE1), arr3(SIZE2);
  97.   fstream f(filename1, ios::out | ios::binary);
  98.  
  99.   // assign values to array arr1
  100.   for (unsigned i = 0; i < arr1.getSize(); i++)
  101.     arr1[i] = 10 * i;
  102.  
  103.   // assign values to array arr3
  104.   for (i = 0; i < SIZE2; i++)
  105.     arr3[i] = i;
  106.  
  107.   cout << "Array arr1 has the following values:\n";
  108.   for (i = 0; i < arr1.getSize(); i++)
  109.     cout << arr1[i] << "  ";
  110.   cout << "\n\n";
  111.  
  112.   // write elements of array arr1 to the stream
  113.   for (i = 0; i < arr1.getSize(); i++)
  114.     arr1.writeElem(f, i);
  115.   f.close();
  116.  
  117.   // reopen the stream for input
  118.   f.open(filename1, ios::in | ios::binary);
  119.  
  120.   for (i = 0; i < arr1.getSize(); i++)
  121.     arr2.readElem(f, i);
  122.   f.close();
  123.  
  124.   // display the elements of array arr2
  125.   cout << "Array arr2 has the following values:\n";
  126.   for (i = 0; i < arr2.getSize(); i++)
  127.     cout << arr2[i] << "  ";
  128.   cout << "\n\n";
  129.  
  130.   // display the elements of array arr3
  131.   cout << "Array arr3 has the following values:\n";
  132.   for (i = 0; i < arr3.getSize(); i++)
  133.     cout << arr3[i] << "  ";
  134.   cout << "\n\n";
  135.  
  136.   // write the array arr3 to file ARRAY3.DAT
  137.   arr3.writeArray(filename2);
  138.   // read the array arr1 from file ARRAY3.DAT
  139.   arr1.readArray(filename2);
  140.  
  141.     // display the elements of array arr1
  142.   cout << "Array arr1 now has the following values:\n";
  143.   for (i = 0; i < arr1.getSize(); i++)
  144.     cout << arr1[i] << "  ";
  145.   cout << "\n\n";
  146.   return 0;
  147. }