如何解决如何在C ++中打开文件以将二进制数据写入其中?
在这里,我有一个使用文件名的写函数,并且有一个指向数组的指针,我想将该数据写到该文件中。如果我执行任何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 举报,一经查实,本站将立刻删除。