Save array object to file - C++ Class

C++ examples for Class:object

Description

Save array object to file

Demo Code

#include <iostream>
#include <fstream>              // for file streams
using namespace std;
class Measure                    // English Measure class
{
   private:/*  w w w . j  a  v  a2 s .c  o m*/
   int feet;
   float inches;
   public:
   Measure() : feet(0), inches(0.0) // constructor (no args)
   {  }                           // constructor (two args)
   Measure(int ft, float in) : feet(ft), inches(in)
   {  }
   void getdist()              // get length from user
   {
      cout << "\n   Enter feet: ";  cin >> feet;
      cout << "   Enter inches: ";  cin >> inches;
   }
   void showdist()             // display distance
   { cout << feet << "\'-" << inches << '\"'; }
};
int main()
{
   char ch;
   Measure dist;                 // create a Measure object
   fstream file;                  // create input/output file
   // open it for append
   file.open("DIST.DAT", ios::binary | ios::app |
   ios::out | ios::in );
   do                             // data from user to file
   {
      cout << "\nMeasure";
      dist.getdist();             // get a distance
      // write to file
      file.write( (char*)&dist, sizeof(dist) );
      cout << "Enter another distance (y/n)? ";
      cin >> ch;
   }
   while(ch=='y');                // quit on 'n'
   file.seekg(0);                 // reset to start of file
   // read first distance
   file.read( (char*)&dist, sizeof(dist) );
   int count = 0;
   while( !file.eof() )           // quit on EOF
   {
      cout << "\nMeasure " << ++count << ": "; // display dist
      dist.showdist();
      file.read( (char*)&dist, sizeof(dist) );  // read another
   }                                         // distance
   cout << endl;
   return 0;
}

Result


Related Tutorials