views:

698

answers:

1

Lets say I have some raster data I want to write to a file.. Now I want to write it as a bmp file..

This data happens to be not DWORD aligned, which, if I understand correctly, needs to be padded with enough bytes to reach the next DWORD..

However, when I try to pad it with this code:

bmFile.Write(0x0, (4-(actualWidth%4)));

I get an error.. If I try to debug (I'm using MSVC++ 6.0), the next statement points to an ASSERT in CFile::Write that asserts the first parameter is NULL.. So this fails..

How should I pad it? should I write out:

bmFile.Write("0x0"(4-(actualWidth%4)));

instead? or would this be treated literally...?

Thanks..

+3  A: 

Perhaps try:

bmFile.Write("\0\0\0\0", (4-(actualWidth%4)));

Your first example is, as you say, trying to write data pointed to by a null pointer. Your second example would write from the bytes '0', 'x', '0' which have ASCII values 0x30, 0x78, 0x30, which is probably not what you intend.

Greg Hewgill
What does \0\0\0\0 do? Write four "0" bits or four "0" bytes?
krebstar
Each \0 represents a single zero byte (each byte is 8 bits). So this provides four bytes from which the Write method can draw from, and the second parameter determines how many of those bytes to write to the file.
Greg Hewgill
Oh I see.. So if I actually ran y our code and the figure from the modulus division evaluated to be, say, 3, I would actually be writing 12 bytes of "0"s? Maybe you can change your example to say "\0" instead of "\0\0\0\0" so I can mark it as the correct answer :)
krebstar
No, the second parameter to Write() is the number of bytes. Even if you pass more bytes than that in the first parameter, only the specified number of bytes will be written. Using just "\0" would be wrong because there wouldn't be enough bytes available to write if you wanted to write 3 bytes.
Greg Hewgill
Really? I see.. This is really helpful, thanks :)
krebstar