Binary file IO

I have been having a very frustrating time trying to work with binary files in c++. I've recently written a simple program that is supposed to take a bitmap file, load it into memory and then dump it into a new file (make a copy).

As far as I can tell, the code is correct. It compiles and runs and makes a new image, but the image is distorted. It turns out that when I am reading the data, the program is interpreting the byte 0x0A as a newline character and reads it in as a carriage return followed by a newline. The input code looks roughly like this:

1
2
3
BYTE *myBlock = new BYTE[filesize];
ifstream inFile("myBitmap.bmp", ios::in | ios::binary);
inFile.read((char *) myBlock, filesize);


Since I have set the ios::binary flag, shouldn't the read function leave the data uninterpreted?

Am I completely nuts, or is it my compiler (I'm using visual c++ 9.0)? Any help is appreciated.
Did you specify the ios::binary flag for the output stream too?
I did, and the output works the way it should, it spits out exactly what I have stored in memory. The problem is when I read the file into memory. If I try reading four bytes, 0xFF 0xD3 0x0A 0x33, the program will store 0xFF 0xD3 0x0D 0x0A.
Never mind, I was completely nuts. I thought I knew what the problem was but I had it backwards, it was reading correctly, it just wasn't writing it correctly.

I thought I had set the ios::binary flag but where I was supposed to write

outFile(filename, ios::out | ios::binary, I wrote instead
outFile(filename, ios::out, ios::binary).

The compiler didn't complain about it unfortunately.
It's all a bit embarrassing, but thank you ropez for your help.
Topic archived. No new replies allowed.