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.
Re: WriteFile writes zero's instead of my bytes
Quote:
Originally Posted by
Vila
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...
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.
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.