问题描述
在这里,我有一个使用文件名的写函数,并且有一个指向数组的指针,我想将该数据写到该文件中。如果我执行任何execpt文件处理,则写入功能将起作用。每当我在该函数中打开文件时,都会导致分段错误。
Bitmap::Bitmap(int width,int height)//:m_width(width),m_height(height),m_pPixel(new uint8_t [width*height*3]);
{
m_width=width;
m_height=height;
m_pixeldata_ptr=new uint8_t [m_width*m_height*3]{0};
}
bool Bitmap::write(string filename)
{
BitMapFileHeader fileHeader;
BitMapInfoHeader infoHeader;
fileHeader.fileSize = sizeof(BitMapFileHeader) + sizeof(BitMapInfoHeader) + m_width*m_height*3;
fileHeader.dataOffest = sizeof(BitMapFileHeader) + sizeof(BitMapInfoHeader);
infoHeader.width=m_width;
infoHeader.height=m_height;
//writing each 3 bit color to bitmap
ofstream file;
file.open(filename,ios::out | ios::binary);
if(!file)//notice not operator is overloaded
{
return false;
}
file.write((char *)&fileHeader,sizeof(BitMapFileHeader));
file.write((char *)&infoHeader,sizeof(BitMapInfoHeader));
file.write((char *)m_pixeldata_ptr,m_width*m_height*3);
file.close();
return true;
}
这是我打电话给的地方。
#include<iostream>
#include "BitMap.h"
using namespace std;
using namespace Mandalbrot;
int main()
{
Mandalbrot::Bitmap bmap(800,600);
bmap.write("test5.bmp");
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)