C++/File/Object Serialization

Материал из C\C++ эксперт
Перейти к: навигация, поиск

reads the inventory file created by the previous program and displays its contents on the screen:

  
#include <iostream>
#include <fstream>
using namespace std;
   
int main()
{
  ifstream in("INVNTRY"); // input
   
  if(!in) {
    cout << "Cannot open INVENTORY file.\n";
    return 1;
  }
   
  char item[20];
  float cost;
   
  in >> item >>  cost;
  cout << item << " " << cost << "\n";
  in >> item >> cost;
  cout << item << " " << cost << "\n";
  in >> item >> cost;
  cout << item << " " << cost << "\n";
   
  in.close();
  return 0;
}


Save object to file with customized operator

  
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
   
class phonebook {
  char name[80];
  char areacode[4];
  char prefix[4];
  char num[5];
public:
  phonebook() { };
  phonebook(char *n, char *a, char *p, char *nm)
  {
    strcpy(name, n);
    strcpy(areacode, a);
    strcpy(prefix, p);
    strcpy(num, nm);
  }
  friend ostream &operator<<(ostream &stream, phonebook o);
  friend istream &operator>>(istream &stream, phonebook &o);
};
   
ostream &operator<<(ostream &stream, phonebook o)
{
  stream << o.name << " ";
  stream << "(" << o.areacode << ") ";
  stream << o.prefix << "-";
  stream << o.num << "\n";
  return stream; // must return stream
}
   
istream &operator>>(istream &stream, phonebook &o)
{
  cout << "Enter name: ";
  stream >> o.name;
  cout << "Enter area code: ";
  stream >> o.areacode;
  cout << "Enter prefix: ";
  stream >> o.prefix;
  cout << "Enter number: ";
  stream >> o.num;
  cout << "\n";
  return stream;
}
   
int main()
{
  phonebook a;
  char c;
   
  fstream pb("phone", ios::in | ios::out | ios::app);
   
  if(!pb) {
    cout << "Cannot open phone book file.\n";
    return 1;
  }
   
  cin >> a;
  cout << "Entry is: ";
  cout << a;  // show on screen
  pb << a;  // write to disk
  char ch;
  pb.seekg(0, ios::beg);
  while(!pb.eof()) {
     pb.get(ch);
     if(!pb.eof()) cout << ch;
  }
  pb.clear();  // reset eof
  cout << endl;
  
  pb.close();
  
}


Using Command-Line Arguments to Get a Filename

  
#include <fstream>
#include <iostream>
using namespace std;
class Animal
{
  public:
     Animal(int weight,long days):itsWeight(weight),DaysAlive(days){}
     ~Animal(){}
     int GetWeight()const { return itsWeight; }
     void SetWeight(int weight) { itsWeight = weight; }
     long GetDaysAlive()const { return  DaysAlive; }
     void SetDaysAlive(long days) { DaysAlive = days; }
  private:
     int itsWeight;
     long DaysAlive;
};
int main(int argc, char *argv[])
{
   if (argc != 2)
   {
      cout << "Usage: " << argv[0] << " <filename>" << endl;
      return(1);
   }
   ofstream fout(argv[1],ios::binary);
   if (!fout)
   {
      cout << "Unable to open " << argv[1] << " for writing.\n";
      return(1);
   }
   Animal Bear(50,100);
   fout.write((char*) &Bear,sizeof Bear);
   fout.close();
   ifstream fin(argv[1],ios::binary);
   if (!fin)
   {
      cout << "Unable to open " << argv[1] << " for reading.\n";
      return(1);
   }
   Animal BearTwo(1,1);
   cout << "BearTwo weight: " << BearTwo.GetWeight() << endl;
   cout << "BearTwo days: " << BearTwo.GetDaysAlive() << endl;
   fin.read((char*) &BearTwo, sizeof BearTwo);
   cout << "BearTwo weight: " << BearTwo.GetWeight() << endl;
   cout << "BearTwo days: " << BearTwo.GetDaysAlive() << endl;
   fin.close();
   return 0;
}