作者:黑客防线网安C/C++教程基地 来源:黑客防线网安C/C++教程基地 浏览次数:0 |
archive.read((reinterpret_cast<char *>(&x), sizeof(x));
archive.read((reinterpret_cast<char *>(&y), sizeof(y));
}
序列化对象
要序列化一个完整的对象,应把每个数据成员写入文件中:
class MP3_clip
{
private:
std::time_t date;
std::string name;
int bitrate;
bool stereo;
public:
void serialize();
void deserialize();
//..
};
void MP3_clip::serialize()
{
{
int size=name.size();// store name's length
//empty file if it already exists before writing new data
ofstream arc("mp3.dat", ios::binary|ios::trunc);
arc.write(reinterpret_cast<char *>(&date),sizeof(date));
arc.write(reinterpret_cast<char *>(&size),sizeof(size));
arc.write(name.c_str(), size+1); // write final '