CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Posts
    20

    WriteFile writes zero's instead of my bytes

    Code in C++:

    short buffer[6];
    buffer[0] = 1;
    buffer[1] = 2;
    buffer[2] = 3;
    buffer[3] = 4;
    buffer[4] = 5;
    buffer[5] = 6;
    short *p = &buffer[0];
    int bufflen = 6;
    int ret;
    WriteFile(g_handle, p, bufflen, &ret, NULL);

    After executing ret = 6 and WriteFile is a success. But when i open file with hex editor i see this:

    01 00 02 00 03 00

    So basically WriteFile wrote my byte then zero, then my byte, then zero again. Is this bug? Or i am doing something wrong? Thanks for help.

  2. #2
    Join Date
    Jul 2007
    Posts
    20

    Re: WriteFile writes zero's instead of my bytes

    Quote Originally Posted by Vila View Post
    Code in C++:

    int bufflen = 6;
    int ret;
    WriteFile(g_handle, p, bufflen, &ret, NULL);
    If I call with bufflen = 12 then ret = 12 and in file:

    01 00 02 00 03 00 04 00 05 00 06 00.

    This is confusing...

  3. #3
    Join Date
    Jul 2007
    Posts
    20

    Re: WriteFile writes zero's instead of my bytes

    I thought that short is 8bit unsigned integer, and it turns out it is 16bit integer. Sorry about that. Will use unsigned char.

  4. #4
    Join Date
    Apr 2010
    Location
    Western WA, USA
    Posts
    59

    Re: WriteFile writes zero's instead of my bytes

    Seems pretty normal to me. You should use:
    Code:
    WriteFile(g_handle, p, sizeof(buffer), &ret, NULL);
    Each 'short' that you write puts two bytes in the file, the low byte followed by the high byte.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured