方法:将一个对象放入一个固定大小的空间中

1、引入

fstream iofile("DiskSystem\\haha.txt",ios::in|ios::out|ios::binary);
char * n = (char *)malloc(1024);
n = "hello worldfdgfdgfdgfdgfdgfdgfd";
iofile.write(n,1024);

iofile.seekg(ios::beg);
char * s = (char *)malloc(1024);
iofile.read(s,1024);
iofile.close();
cout<<s<<endl;

 

2、换成对象

class Test
{
public:
Test(char * c = "Test"):s(c){};
char *s;
};

Test * t = (Test *)malloc(1024);
t->s = "Hello World";

fstream iofile("DiskSystem\\haha.dat",ios::in|ios::out|ios::binary);
iofile.write((char *)t,1024);

Test * tt = (Test *)malloc(1024);
iofile.seekg(ios::beg);
iofile.read((char *)tt,1024);

cout<<tt->s<<endl;

iofile.close();

你可能感兴趣的:(对象)