64,654
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <fstream>
using namespace std;
struct RGB
{
int r;
int g;
int b;
};
struct MyStruct
{
int i;
RGB rgb[1];
};
int _tmain(int argc, _TCHAR* argv[])
{
MyStruct* pMyStruct = nullptr;
pMyStruct = new MyStruct[sizeof(int) + sizeof(RGB) * 4];
int i = 7;
RGB rgb[4];
for (int k = 0; k < 4; k++)
{
rgb[k].r = 10 + k;
rgb[k].g = 11 + k;
rgb[k].b = 12 + k;
}
ofstream fout("out.bin", ios_base::binary | ios_base::out);
fout.write((char *)&i, sizeof(int));
for (int k = 0; k < 4; k++)
{
fout.write((char *)&rgb[k], sizeof(RGB));
}
fout.close();
ifstream fin("out.bin", ios_base::binary | ios::in);
fin.read((char *)pMyStruct, sizeof(int) + sizeof(RGB) * 4);
fin.close();
if(pMyStruct != nullptr)
delete pMyStruct;
return 0;
}
class A
{
B b;
C c[1];
};
class B{};
class C{};