Serialize object Demo : Object Serialize « File « C++

C++
1. Bitset
2. Class
3. Console
4. Data Structure
5. Data Type
6. Deque
7. Development
8. File
9. Function
10. Generic
11. Language
12. List
13. Map Multimap
14. Overload
15. Pointer
16. Queue Stack
17. Set Multiset
18. STL Algorithms Binary search
19. STL Algorithms Heap
20. STL Algorithms Helper
21. STL Algorithms Iterator
22. STL Algorithms Merge
23. STL Algorithms Min Max
24. STL Algorithms Modifying sequence operations
25. STL Algorithms Non modifying sequence operations
26. STL Algorithms Sorting
27. STL Basics
28. String
29. Valarray
30. Vector
Java
XML
XML Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C++ » File » Object SerializeScreenshots 
Serialize object Demo
Serialize object Demo

#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

#define SIZE 40

class Product {
  char item[SIZE]
  int onhand;      
  double cost;     
public:
  Product(char *i, int o, double c)
  {
    strcpy(item, i);
    onhand = o;
    cost = c;
  }
  void store(fstream &stream);
  void retrieve(fstream &stream);
  friend ostream &operator<<(ostream &stream, Product ob);
  friend istream &operator>>(istream &stream, Product &ob);
};

ostream &operator<<(ostream &stream, Product ob)
{
  stream << ob.item << ": " << ob.onhand;
  stream << " on hand at $" << ob.cost << '\n';

  return stream;
}

istream &operator>>(istream &stream, Product &ob)
{
  cout << "Enter item name: ";
  stream >> ob.item;
  cout << "Enter number on hand: ";
  stream >> ob.onhand;
  cout << "Enter cost: ";
  stream >> ob.cost;

  return stream;
}

void Product::store(fstream &stream)
{
  stream.write(item, SIZE);
  stream.write((char *&onhand, sizeof(int));
  stream.write((char *&cost, sizeof(double));
}

void Product::retrieve(fstream &stream)
{
  stream.read(item, SIZE);
  stream.read((char *&onhand, sizeof(int));
  stream.read((char *&cost, sizeof(double));
}

int main()
{
  fstream fileStream("fileStream", ios::out | ios::binary)
  int i;

  Product productObject1("p"124.95);
  Product productObject2("h"59.45);
  Product productObject3("w"2213.90);
  Product productObject4(""00.0);

  if(!fileStream) {
    cout << "Cannot open file for output.\n";
    return 1;
  }
  // write to file
  productObject1.store(fileStream);
  productObject2.store(fileStream);
  productObject3.store(fileStream);

  fileStream.close();

  // open for input
  fileStream.open("fileStream", ios::in | ios::binary)

  if(!fileStream) {
    cout << "Cannot open file for input.\n";
    return 1;
  }

  do {
    cout << "Record # (-1 to quit): ";
    cin >> i;
    if(i == -1
       break;
    fileStream.seekg(i*(SIZE+sizeof(int)+sizeof(double)), ios::beg);
    productObject4.retrieve(fileStream);
    cout << productObject4;
  while(fileStream.good());

  fileStream.close();

  return 0;
}


           
       
Related examples in the same category
1. Save and read class back and forth to a fileSave and read class back and forth to a file
w_w___w__._j__a___v___a2s___._c__om_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.