简体   繁体   中英

ReadFile function lpbuffer and bytes to read clarifcation

I'm pretty new to this so any clarification would be appreciated. When using the function ReadFile, how does the type of the lpBuffer interact with the parameter of "number of bytes to read"?

For instance what if you had an unsigned short MyShort[5] as lpBuffer, and then you set bytes to read as 2. Will all data be stored in MyShort[0]? Or would the first byte go into MyShort[0] and the second byte go into MyShort[1]? What happens when you set bytes to read is increase say to 9? Will 16bits go into MyShort[0] and then 16 more into MyShort[1] etc...?

Thanks

lpBuffer is always treated as a pointer to an array of specified amount of bytes ( nNumberOfBytesToRead ). The amount of bytes actually read will be stored in the variable pointed to by lpNumberOfBytesRead parameter or as async (overlapped) result later. So in your case if you request to read 2 bytes it may either read two bytes storing both of them in MyShort[0] , or just a single byte stored in lower half of MyShort[0] or nothing at all. If you request to read 9 bytes then it will ready up to 9 bytes storing 2 + 2 + 2 + 2 + 1 bytes sequentially.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM