나는, 구조체 B는 내가 쓰기에게하고 싶은거야 구조체읽기/이진 파일에 복잡한 구조체 쓰기
typedef struct a_t
{
vector <int> a;
int p;
}a;
typedef struct b_t
{
a x;
int y;
}b;
구조체 A는 구조체 벡터를 포함하는 다음과 같은 구조체를 포함 한/A로 구조체 B를 읽어 이진 파일. 그것은 옆에 : 다음 코드는 오류 메시지가 입니다
int main()
{
b m;
m.x.a.push_back(1);
m.x.a.push_back(2);
m.x.a.push_back(3);
m.x.p = 5;
m.y = 7;
cout << sizeof(m.y) << endl;
cout << sizeof(m.x) << endl;
cout << sizeof(m) << endl;
ofstream outfile("model",ios::out|ios::binary);
outfile.write((char*)&m,sizeof(m));
outfile.close();
b p;
ifstream iffile("model", ios::in|ios::binary);
iffile.read((char*)&p,sizeof(a));
iffile.close();
cout << p.y << endl;;
cout << p.x.p << endl;
cout << p.x.a[0] << endl;
cout << p.x.a[1] << endl;
cout << p.x.a[2] << endl;
return 0;
}
작동하지 않습니다 "0x0000000000504010 * * 중단됨 (덤프 코어) * glibc에는 더블 무료 또는 손상 (상단) 감지" struct에 파일을 쓰지 않습니다.
가능하면'std :: vector'에'std :: array'를 사용하십시오. 'std :: vector'는 동적으로 할당 된 메모리를 관리하지만'std :: array'는 동적으로 할당 된 메모리를 관리하지 않습니다. 이 경우이 작업을 수행하는 순진한 방법 (char * 및 write/read 캐스팅)을 수행하게됩니다. – Kevin