写入文件时出现分段错误,代码在下面
我正在以二进制模式打开文件。我对数组有很多结构,然后将数据写入文件(名为sample.avi)
ofstream AVI;
AVI.open("sample.avi",ios::binary);
if(!AVI)
{
cout << "AVI file not opening"<<endl;
return 0;
}
long long int size = 0;
size = sizeof(List) + sizeof(List) + sizeof(AVIMainHeader) + sizeof(List) + sizeof(AVIStreamHeader) + sizeof(BitmapInfoHeader) + sizeof(BitmapInfoHeader) + sizeof(List);
cout << size;
uint8_t *AVIFile = new uint8_t[300];
List riff = MakeRiffHeader();
List hdrl = MakeHdrlHeader();
AVIMainHeader avih = MakeAvihHeader();
List strl = MakeStrlHeader();
AVIStreamHeader strh = MakeStrhHeader();
BitmapInfoHeader bit = MakeBitHeader();
List movi = MakeMoviHeader();
long long int length1 = 0;
memcpy(&AVIFile[length1], (const void*)&riff, sizeof(List));
length1 += sizeof(List);
memcpy(&AVIFile[length1], (const void*)&hdrl, sizeof(List));
length1 += sizeof(List);
memcpy(&AVIFile[length1], (const void*)&avih, sizeof(AVIMainHeader));
length1 += sizeof(AVIMainHeader);
memcpy(&AVIFile[length1], (const void*)&strl, sizeof(List));
length1 += sizeof(List);
memcpy(&AVIFile[length1], (const void*)&strh, sizeof(AVIStreamHeader));
length1 += sizeof(AVIStreamHeader);
memcpy(&AVIFile[length1], (const void*)&bit, sizeof(BitmapInfoHeader));
length1 += sizeof(BitmapInfoHeader);
memcpy(&AVIFile[length1], (const void*)&movi, sizeof(List));
length1 += sizeof(List);
AVI.write(reinterpret_cast<char*>(AVIFile),length1);
Chunk vidf;
vidf.Ckid = MakeFourCC("00db");
vidf.CkSize = 2048 * 1537;
AVI.write(reinterpret_cast<char*>(&vidf), sizeof(vidf));
AVI.write(reinterpret_cast<char*>(red), 2048 * 1537);
AVI.close();
return 0;
我从writing(AVI.write(reinterpret_cast<char*>(AVIFILE), length1))
开始的行以及下面的行似乎是分段错误的根源(就像我们注释它一样,这没有错)。
请指出我错了
编辑:
uint8_t *BUFFER = new uint8_t[length];
uint8_t *BUFFER1 = new uint8_t[(3 * length) / 2];
uint8_t *final_image = new uint8_t[(3 * length) / 2];
uint8_t *red = new uint8_t[2048 * 1546];
uint8_t *green1 = new uint8_t[2048 * 1546];
uint8_t *green2 = new uint8_t[2048 * 1546];
uint8_t *blue = new uint8_t[2048 * 1546];
答案 0 :(得分:0)
AVI.write(reinterpret_cast<char*>(&red), 2048 * 1537);
应该是
AVI.write(reinterpret_cast<char*>(red), 2048 * 1537);
当您拥有一个指针时,无需添加&
即可获得指向该地址的地址,只需使用指针本身即可。