i am trying to open a bitmap file in c++, change it and then create another bitmap file. the problem is when i try to output a bitmap file with ofstream, even without changing any byte of file, it has a strange output.
#include <fstream>
using namespace std;
int main ()
{
ifstream fin ("6.bmp", std::ios::binary);
ofstream fout ("output.bmp");
for (int i = 0; i < 1920*1080*3 + 54; i++)
fout.put(fin.get());
}
1920 * 1080 is size of picture and *3 for RED, GREEN, BLUE. i know that a bitmap file have a 54-byte header, so i think there are exactly 1920*1080*3 + 54 bytes in 6.bmp;
when the input is : input
then i get this for output.
(i resized both pictures for uploading them but they both are 1920*1080)
i compiled this code with g++ in windows 8.1. and i don't want to use bitmap library.
With Windows, fout also must be opened in binary mode, otherwise a 0x0d byte will be auto-inserted before every 0x0a byte, corrupting the output file.