ofstream ofs("aaa.txt",ios::binary);
Record* buff2 = new Record[3];
buff2[0].PA = buff2[0].PAB = buff2[0].PB = 0;
buff2[1].PA = buff2[1].PAB = buff2[1].PB = 1;
buff2[2].PA = buff2[2].PAB = buff2[2].PB = 1;
ofs.write(reinterpret_cast<char*>(&buff2),BUFFER_SIZE);
ofs.flush();
buff2[0].PA = buff2[0].PAB = buff2[0].PB = 4;
buff2[1].PA = buff2[1].PAB = buff2[1].PB = 5;
buff2[2].PA = buff2[2].PAB = buff2[2].PB = 6;
ofs.seekp(0,ios::end);
ofs.write(reinterpret_cast<char*>(&buff2),BUFFER_SIZE);
ofs.close();
Struktura to :
struct Record{
float PA;
float PB;
float PAB;
};
czyli ma 12 bajtów. BUFFER_SIZE ma 36, ale może być wielokrotnościa sizeof(Record).Chciałbym zapisać dane do pliku i potem je odczytać.
Odczytuje tak:
Record* buff = new Record[3];
ifstream ifs("aaa.txt",ios::binary);
for(int i=0,j=0;i<2;++i){
ifs.seekg(i*BUFFER_SIZE);
ifs.read(reinterpret_cast<char*>(&buff),12);
cout<<buff[0].PA<<" "<<buff[0].PB<<" "<<buff[0].PAB<<"\n";
cout<<buff[1].PA<<" "<<buff[1].PB<<" "<<buff[1].PAB<<"\n";
cout<<buff[2].PA<<" "<<buff[2].PB<<" "<<buff[2].PAB<<"\n";
}
ifs.close();
Output:
4 4 4
5 5 5
6 6 6
4 4 4
5 5 5
6 6 6
a powinno być:
0 0 0
1 1 1
1 1 1
4 4 4
5 5 5
6 6 6
Gdy zapsiuje dwie rózne tablice(najpierw buff2) a potem np. buff3 to wynik jest prawidłowy. Moim zdaniem problem jest flush bo jak ustawie breakpoint'a za nim to plik się tworzy ale ma 0KB.