
/*----------------------------------------------------------------------*/
/*                     BinaryFile.cpp                                   */
/* Authored By : H.P.Kasmaei                                            */
/* Last Update : Dec 10, 2011                                           */
/* http://www.hpkclasses.ir/                                            */
/*----------------------------------------------------------------------*/

#include <iostream.h>
#include <fstream.h>

int main() {
	
   typedef struct {    
		char chr;    
		int num; 
	} ExampleStruct;

	char chr = 'A';    
	int num = 1;    

	ExampleStruct s = {'B', 2};    
	
    ofstream f("example.bin", ios::binary);

	//fstream f;
	//f.open("st.dat", ios::binary);

	f.write((char *)(&chr), sizeof(chr));    
	f.write((char *)(&num), sizeof(num));    
	f.write((char *)(&s), sizeof(s));    
	f.close(); 
	
	ifstream g("example.bin", ios::binary);     
	
	g.read((char *)(&chr), sizeof(chr));    
	cout << "chr:" << chr << endl;     
	g.read((char *)(&num), sizeof(num));    
	cout << "num:" << num << endl;     
	g.read((char *)(&s), sizeof(s));    
	cout << "s.chr:" << s.chr << endl;    
	cout << "s.num:" << s.num << endl;     
	g.close();

    return 0;
}
