include <iostream>
include <fstream>
include <string>
void read();
using namespace std;
struct S{
char c;
bool b;
char addr[100];
};
class A{
int date;
public:
A(int d = 0):date(d){}
void show(){
cout << "date = "<< date <<endl;
}
};
int main()
{
int n = 123;
double d = 4.56;
S s={'S',true,"beijing"};
A a(789);
ofstream fout("var.dat");
fout.write((char)&n,sizeof(n));
fout.write((char)&d,sizeof(d));
fout.write((char)&s,sizeof(s));
fout.write((char)&a,sizeof(a));
fout.close();//read();
ifstream fin("var.dat");
fin.read((char)&n,sizeof(n));
fin.read((char)&d,sizeof(d));
fin.read((char)&s,sizeof(s));
fin.read((char)&a,sizeof(a));
fin.close();
cout << "n = " <<n <<endl;
cout << "d = " <<d <<endl;
cout << "s.b = " <<s.b <<endl;
cout << "a.addr = " <<s.addr <<endl;
a.show();
}
void read()
{
/* ifstream fin("var.dat");
string str;
if(!fin)
{
cout << "read error" <<endl;
}
string s;
while(getline(fin,s,'\n'))
{
cout<<s<<endl;
}
*/
//这样读取不行,会产生乱码
}