一个对象怎么把自身存入文件以及从文件取出
一个对象如何把自身存入文件以及从文件取出?
我想写个画数据折线图的小程序,有些东西还没有想好:
在SavaToFile()里,我想用ofstream.write((char *)this,sizeof(*this))把对象自身存入文件,可行吗?对象大小是不是sizeof(*this)?
在LoadFromFile()里,如何用ifstream.read()读取文件中数据写入自身对象中?读取大小又是如何判断?
------解决思路----------------------
带有指针的数据结构(比如 vector)都得单独把内容输出的,ofstream.write((char *)this,sizeof(*this)) 这种只对内建类型才能用。
------解决思路----------------------
搜“序列化 反序列化”
我想写个画数据折线图的小程序,有些东西还没有想好:
#ifndef _DataGraph_H
#define _DataGraph_H
class DataGraph
{
private:
int Amplitude;
float timeInterval;
Point prevPoint,curPoint;
unsigned pointsCount;
vector<Point> points;
public:
DataGraph();
~DataGraph();
void setAmplitude();
void setTimeInterval();
Point getPoint();
Point pointToViewPoint();
void line();
void drawGraph();
void SavaToFile();
void LoadFromFile();
}
extern DataGraph aDataGraph;
#endif
在SavaToFile()里,我想用ofstream.write((char *)this,sizeof(*this))把对象自身存入文件,可行吗?对象大小是不是sizeof(*this)?
在LoadFromFile()里,如何用ifstream.read()读取文件中数据写入自身对象中?读取大小又是如何判断?
------解决思路----------------------
带有指针的数据结构(比如 vector)都得单独把内容输出的,ofstream.write((char *)this,sizeof(*this)) 这种只对内建类型才能用。
------解决思路----------------------
搜“序列化 反序列化”