-
December 31st, 2014, 08:45 AM
#1
Copying one bmp file to another using fstream, Access violation reading location er
I would like to copy one bmp file to another using plain fstream (none 3-rd party library). I've managed to come up with something but I keep getting error:
Unhandled exception at 0x504A3442 (msvcr120d.dll) in ConsoleApplication1.exe: 0xC0000005: Access violation reading location 0x00380000.
when i try to write the content of the bmp files not the header, namely error occurs in this line:
Code:
ofs_differential.write((char*)&picture, (bfh_warm.bfSize - bfh_warm.bfOffBits));
Also i am not sure if i am doing it in a right way, meaning that even after solving this error i will copy the bmp file sucessfully.
Code:
Code:
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
struct BITMAPfileHEADER
{
unsigned short bfType;
unsigned int bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned int bfOffBits;
};
struct BITMAPinfoHEADER
{
unsigned int biSize;
unsigned int biWidth;
unsigned int biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
unsigned int biXpelsPerMeter;
unsigned int biYpelsPerMeter;
unsigned int biClrUses;
unsigned int biClrImportant;
};
int readBFH(ifstream &ifs, BITMAPfileHEADER &bfh)
{
ifs.read(reinterpret_cast<char *>(&bfh.bfType), 2);
ifs.read(reinterpret_cast<char *>(&bfh.bfSize), 4);
ifs.read(reinterpret_cast<char *>(&bfh.bfReserved1), 2);
ifs.read(reinterpret_cast<char *>(&bfh.bfReserved2), 2);
ifs.read(reinterpret_cast<char *>(&bfh.bfOffBits), 4);
return ifs.tellg();
}
int readBIH(ifstream &ifs, BITMAPinfoHEADER &bih)
{
ifs.read(reinterpret_cast<char *>(&bih.biSize), 4);
ifs.read(reinterpret_cast<char *>(&bih.biWidth), 4);
ifs.read(reinterpret_cast<char *>(&bih.biHeight), 4);
ifs.read(reinterpret_cast<char *>(&bih.biPlanes), 2);
ifs.read(reinterpret_cast<char *>(&bih.biBitCount), 2);
ifs.read(reinterpret_cast<char *>(&bih.biCompression), 4);
ifs.read(reinterpret_cast<char *>(&bih.biSizeImage), 4);
ifs.read(reinterpret_cast<char *>(&bih.biXpelsPerMeter), 4);
ifs.read(reinterpret_cast<char *>(&bih.biYpelsPerMeter), 4);
ifs.read(reinterpret_cast<char *>(&bih.biClrUses), 4);
ifs.read(reinterpret_cast<char *>(&bih.biClrImportant), 4);
return ifs.tellg();
}
char* readPictureData(ifstream &ifs, unsigned int size, int cursor)
{
ifs.seekg(cursor, ios::beg);
char *picture = new char[size];
ifs.read(reinterpret_cast<char *>(picture), size);
return picture;
}
int main()
{
BITMAPfileHEADER bfh_warm;
BITMAPfileHEADER bfh_cold;
BITMAPinfoHEADER bih_warm;
BITMAPinfoHEADER bih_cold;
int cursor_warm;
int cursor_cold;
ifstream ifs_warm("1.bmp", ios::binary);
ifstream ifs_cold("2.bmp", ios::binary);
ofstream ofs_differential("differential.bmp", ofstream::binary);
cursor_warm = readBFH(ifs_warm, bfh_warm);
cursor_warm = readBIH(ifs_warm, bih_warm);
cursor_cold = readBFH(ifs_cold, bfh_cold);
cursor_cold = readBIH(ifs_cold, bih_cold);
char* picture = readPictureData(ifs_warm, bfh_warm.bfSize - bfh_warm.bfOffBits, bfh_warm.bfOffBits);
ofs_differential.write((char*)&bfh_warm, sizeof(bfh_warm)); //write header
ofs_differential.write((char*)&bih_warm, sizeof(bih_warm)); //write header
ofs_differential.write((char*)&picture, (bfh_warm.bfSize - bfh_warm.bfOffBits));
ifs_warm.close();
ifs_warm.close();
ofs_differential.close();
return 0;
}
-
December 31st, 2014, 09:34 AM
#2
Re: Copying one bmp file to another using fstream, Access violation reading location
Code:
ofs_differential.write((char*)picture, ...);
picture is already pointer, remove unnecessary &
-
January 1st, 2015, 05:35 PM
#3
Re: Copying one bmp file to another using fstream, Access violation reading location
Ok it helped, it was my mistake i didn't notice this little thing ;P
However, the header of the output file seems to be corrupted, because it's what i get when i try to open the output image. Does anyone happen to know why?
-
January 2nd, 2015, 06:23 AM
#4
Re: Copying one bmp file to another using fstream, Access violation reading location
Something to check:
Code:
ofs_differential.write((char*)&bfh_warm, sizeof(bfh_warm) ); //write header
What is sizeof(bfh_warm) ? Is it 14, or 16 ?
Visual C++ has bitmap header structures you might use: BITMAPFILEHEADER and BITMAPINFOHEADER
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|