-
fwrite buffer problem
Hello, I have been stuck on this for hours no joke :( I am a guy who is a noob when it comes to C + +.
i want to transfer bitmap data into a char/string/stream (any of the above) buffer, but it doesn't work.
What does work is writing the data to a file like this:
fwrite(&BmHdr,sizeof(BITMAPFILEHEADER),1,myfile);
BmHdr is a BITMAPFILEHEADER
But, I want to write it into a chair,string,or stream, so I have tried:
memcpy(Buffer,&BmHdr,Length);
(buffer being an unsigned char)
when doing this, the first few things are as it should be (it shows like bm1/2)
but it chops it off there, when it should have 14 characters (bm1/2 , then some nulls and another character).
The Length parameter doesn't seem to be the problem.
One of the many other things I've tried include:
-trying to transfer into a stringstream (same/similar problem)
-setbuf() - trying to redirect the fwrite output, which works, into a buffer, which doesnt work
Thanks, and any help would be greatly appreciated
-
You can write what you like to a char array, providing there is room in the array.
But as soon as you try to use any str... functions on it, it's always going to stop at the first \0.
If you're trying to generate some kind of printable version of your header, then you need to do things like
cout << BmHdr.signature;
cout << BmHdr.width;
etc.
Or you do the same thing to a stringstream.
-
Thanks for the reply. I'm trying to get the whole binary into the buffer.
the code I tried to make, which now works a little bit better, but still messes up somewhere
Code:
void *bits = malloc(BitInfo.bmiHeader.biSizeImage);
GetDIBits(DestDC,DestBitmap,0,BitInfo.bmiHeader.biHeight,bits,&BitInfo,DIB_PAL_COLORS);
fstream FileStream;
FileStream.open("fail.bmp", ios::out|ios::binary);
FileStream<<(BYTE*)&bitheader<<(BYTE*)&BitInfo.bmiHeader<<(BYTE*)bits;
FileStream.close();
original Code which works:
Code:
void *bits = malloc(BitInfo.bmiHeader.biSizeImage);
GetDIBits(DestDC,DestBitmap,0,BitInfo.bmiHeader.biHeight,bits,&BitInfo,DIB_PAL_COLORS);
HANDLE bmfile = CreateFile("image.bmp",GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(bmfile,&bitheader,sizeof(BITMAPFILEHEADER),&bytes,NULL);
WriteFile(bmfile,&BitInfo.bmiHeader,sizeof(BITMAPINFOHEADER),&bytes,NULL);
WriteFile(bmfile,bits,BitInfo.bmiHeader.biSizeImage,&bytes,NULL);
comparing the two binaries side by side (mine is fail.bmp)
mine is 18 kb, while original is 60kb.
http://i.imgur.com/eQ6iO.png
Any idea whats going wrong?
-
So stop trying to use strings for things which are NOT strings.
Posting some actual code, not random lines, might actually help us to tell you specifically where you're going wrong.
-
Specifically, you can't do this:
Code:
FileStream<<(BYTE*)&bitheader<<(BYTE*)&BitInfo.bmiHeader<<(BYTE*)bits;
Instead you must use the fstream write method.
-
YAY!! , now i can use strings with that method, and they work. Thanks for everyone!