1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int streetnum=10;
struct house
{
int no;
string newspaper[5];
double price[5];
};
class street
{
string streetname;
house housedetail[10];
public:
street (){};
street (string a, house b[], int size)
{
setstreetname(a);
sethouse(b, size);
}
void setstreetname (string a)
{ streetname=a; }
void sethouse (house a[], int size)
{
for (int i=0; i<size; i++)
housedetail[i]=a[i];
}
string getstreetname()
{ return streetname; }
};
int main ()
{
fstream usefile( "data.dat", ios::out | ios::binary );
if ( !usefile )
{
cout << "File could not be opened." << endl;
}
house house1[5]={
{31,{"Berita Harian ","New Straits Times"},{1.4,1.3}},
{32,{"Sin Chew Daily "},{1.3}},
{33,{"The Star ","Sin Chew Daily "},{1.3,1.3}}
};
street street1("Jalan Merah", house1, 5);
usefile.seekp(0);
usefile.write( reinterpret_cast<const char * >(&street1),sizeof(street));
system ("pause");
return 0;
}
|