Like Tree3Likes
  • 1 Post By Salem
  • 1 Post By Salem
  • 1 Post By iMalc

fwrite buffer problem

This is a discussion on fwrite buffer problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I have been stuck on this for hours no joke I am a guy who is a noob when ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    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
    Last edited by TenderKitten; 10-15-2011 at 09:54 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    TenderKitten likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    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?
    Last edited by TenderKitten; 10-15-2011 at 11:35 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    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.
    TenderKitten likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Specifically, you can't do this:
    Code:
    FileStream<<(BYTE*)&bitheader<<(BYTE*)&BitInfo.bmiHeader<<(BYTE*)bits;
    Instead you must use the fstream write method.
    TenderKitten likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    YAY!! , now i can use strings with that method, and they work. Thanks for everyone!
    Last edited by TenderKitten; 10-16-2011 at 10:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-27-2009, 05:56 PM
  2. Problem using fwrite
    By kenkoh in forum C Programming
    Replies: 10
    Last Post: 02-16-2008, 09:34 AM
  3. fwrite problem
    By recluse in forum C Programming
    Replies: 5
    Last Post: 05-02-2005, 10:00 AM
  4. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 06:57 PM
  5. some problem with fwrite
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-01-2002, 05:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21